0

次のエラーが表示されます。

Routing Error

No route matches {:controller=>"tasks", :action=>"complete", :list_id=>1, :id=>nil}
Try running rake routes for more information on available routes.

これは、routes.rb ファイルにあるものです。

resources :lists do 
  resources :tasks
end

match 'lists/:list_id/tasks/:id/complete' => 'tasks#complete', :as => :complete_task

root :to => 'lists#index'

私のtasks_controllerで:

attr_accessor :completed
before_filter :find_list

def create
  @task = @list.tasks.new(params[:task])
  if @task.save
    flash[:notice] = "Task created"
redirect_to list_url(@list)
  else
flash[:error] = "Could not add task at this time."
redirect_to list_url(@list)
  end
end

def complete
  @task = @list.tasks.find(params[:id])
  @task.completed = true
  @task.save
  redirect_to list_url(@list)
end

private
  def find_list
    @list = List.find(params[:list_id])
  end

そして、show.html.erb (エラーが発生した場所) で:

<%= button_to "Complete", complete_task_path(@list.id,task.id) %>

誰かが私が間違っていることを教えてもらえますか?

4

1 に答える 1

1

What's causing the problem is that task.id in your show view returns nil, while in your routes:

match 'lists/:list_id/tasks/:id/complete' => 'tasks#complete', :as => :complete_task

Requires a task id in order to match the url pattern.

You can read more about it in this blog post.

于 2012-10-03T23:03:19.653 に答える