0

当我将提要部分包含在我的主页中时,代码可以正常工作。但是,当我将删除链接添加到提要项时,我不断收到“未定义方法 'todo_path'”错误。有谁能够帮我?

这是我的 Rspec 失败:

1) Todo pages todo destruction as correct user should delete a todo
 Failure/Error: before { visit root_path }
 ActionView::Template::Error:
   undefined method `todo_path' for #<#<Class:0x00000004eac910>:0x00000004eb2b08>

这是我在本地主机上的错误:

8:      Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
9:  </span>
10:     <% if current_user?(feed_item.user) %>
11:         <%= link_to "delete", feed_item, method: :delete, data: { confirm:  'Are you sure?' } %>
12:     <% end %>
13: </li>

这是我的 _feed_item.html.erb:

<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<span class="user">
    <%= link_to feed_item.user.username, feed_item.user %>
</span>
<span class="entry"><%= feed_item.entry %></span>
<span class="timestamp">
    Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
<% if current_user?(feed_item.user) %>
    <%= link_to "delete", feed_item, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
</li>

这是我的 TodosController.rb:

class TodosController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy

# GET /todos
# GET /todos.json
def index
#@user = User.find(params[:user_id])
@user = User.find_by_id(params[:user_id])
@todo = @user.todos

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @todos }
end
end

# GET /todos/1
# GET /todos/1.json
def show
  @todo = Todo.find(params[:id])
  @todo.user = User.find_by_id(params[:user_id])
  @user = @todo.user
respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @todo }
end
end

# GET /todos/new
# GET /todos/new.json
def new
  @user = current_user
  @todo = Todo.new

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @todo }
end
end

# GET /todos/1/edit
def edit
  @todo = Todo.find(params[:id])
  @todo.user = User.find_by_id(params[:user_id])
  @user = @todo.user
end

# POST /todos
# POST /todos.json
def create
  @todo = current_user.todos.build(params[:todo])
  #@todo.user = User.find_by_id(params[:user_id])
  @todo.user = current_user
  @user = @todo.user
respond_to do |format|
  if @todo.save
    #format.html { redirect_to user_todo_path(@user, @todo), notice: 'Todo was      successfully created.' }
    format.html { redirect_to user_todo_path(@user, @todo), notice: 'Todo was successfully created!' }
    format.json { render json: @user, status: :created, location: @user }
  else
    @feed_items = []
    format.html { render action: "new" }
    format.json { render json: @todo.errors, status: :unprocessable_entity }
  end
end
end

# PUT /todos/1
# PUT /todos/1.json
def update
  @todo = Todo.find(params[:id])

respond_to do |format|
  if @todo.update_attributes(params[:todo])
    format.html { redirect_to @todo, notice: 'Todo was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @todo.errors, status: :unprocessable_entity }
  end
end
end

# DELETE /todos/1
# DELETE /todos/1.json
def destroy
  @todo.destroy

respond_to do |format|
  format.html { redirect_to root_url }
  format.json { head :no_content }
end
end

private

def correct_user
  @todo = current_user.todos.find_by_id(params[:id])
  redirect_to root_url if @todo.nil?
end
end

编辑:这是我的规范文件(todo_pages_spec.rb):

require 'spec_helper'

describe "Todo pages" do

subject { page }

let(:user) { FactoryGirl.create(:user) }
before { sign_in user }

describe "todo creation" do
before { visit new_user_todo_path(user) }

describe "with invalid information" do

    it "should not create a todo" do
        expect { click_button 'Submit' }.not_to change(Todo, :count)
    end

    describe "error messages" do
        before { click_button 'Submit' }
        it { should have_content('error') }
    end
end

describe "with valid information" do

    before{ fill_in 'Entry', with: "Lorem ipsum" }
    it "should create a todo" do
        expect { click_button 'Submit' }.to change(Todo, :count).by(1)
    end
end
end

describe "todo destruction" do
  before { FactoryGirl.create(:todo, user: user) }

describe "as correct user" do
  before { visit root_path }

  it "should delete a todo" do
    expect { click_link "delete" }.to change(Todo, :count).by(-1)
  end
end
end
end

这是我的 routes.rb:

    home_index GET    /home/index(.:format)                    home#index
user_todos GET    /users/:user_id/todos(.:format)          todos#index
           POST   /users/:user_id/todos(.:format)          todos#create
new_user_todo GET    /users/:user_id/todos/new(.:format)      todos#new
edit_user_todo GET    /users/:user_id/todos/:id/edit(.:format) todos#edit
 user_todo GET    /users/:user_id/todos/:id(.:format)      todos#show
           PUT    /users/:user_id/todos/:id(.:format)      todos#update
           DELETE /users/:user_id/todos/:id(.:format)      todos#destroy
     users GET    /users(.:format)                         users#index
           POST   /users(.:format)                         users#create
  new_user GET    /users/new(.:format)                     users#new
 edit_user GET    /users/:id/edit(.:format)                users#edit
      user GET    /users/:id(.:format)                     users#show
           PUT    /users/:id(.:format)                     users#update
           DELETE /users/:id(.:format)                     users#destroy
  sessions POST   /sessions(.:format)                      sessions#create
new_session GET    /sessions/new(.:format)                  sessions#new
   session DELETE /sessions/:id(.:format)                  sessions#destroy
    signup        /signup(.:format)                        users#new
    signin        /signin(.:format)                        sessions#new
   signout DELETE /signout(.:format)                       sessions#destroy
      root        /                                        home#index

非常感谢您!

4

1 回答 1

0

我认为问题出feed_item在你的部分。我不是 Rails 这方面的专家,但我希望user_todo_path(feed_item.id) 它是todo_path基于对项目的单一引用生成的ToDo,但没有这样的无参数路线。

于 2013-06-22T21:45:15.517 回答