CH。 10.3.4 RoR教程未定义的方法'todo_path'

时间:2013-06-18 19:53:18

标签: ruby-on-rails railstutorial.org

当我将Feed部分包含到我的主页时,代码工作正常。但是当我将删除链接添加到feed项时,我继续收到'未定义的方法'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>

这是我在localhost上的错误:

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

编辑: 这是我的Spec文件(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

提前非常感谢你!

1 个答案:

答案 0 :(得分:0)

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