我有一个项目,其中包含具有待办事项
和任务
的项目
。当我尝试创建新任务时,在提交时收到此错误:
No route matches [POST] "/projects/1/todos/19/tasks/new"
这是我的表格:
<%= form_for [@todo, @todo.tasks.new], :url => new_project_todo_task_path(@project, @todo) do |f| %>
<div class="field">
<%= f.label :description, "Description" %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %> or <%= link_to "Cancel", "#", :id => "cancel_new_task_link" %>
</div>
<% end %>
这是我的 Controller :
class TasksController < ApplicationController
before_filter :authenticated?
before_filter :get_project_and_todo
respond_to :html, :xml, :json, :js
def new
@task = @todo.tasks.new
end
def create
@task = @todo.tasks.new(params[:task])
if @task.save
respond_with @todo, :location => project_todo_path(@project, @todo)
else
render "new"
end
end
private
def get_project_and_todo
@project = Project.find(params[:project_id])
@todo = @project.todos.find(params[:todo_id])
end
end
这是我的路线:
resources :projects do
resources :todos do
resources :tasks
end
end
谢谢
最佳答案
您的网址不应为 new_project_todo_task_path(@project, @todo)
。您无需在此处指定 URL,因为 Rails 会从传递给 form_for
的参数中暗示它。
如果最终对象是一个新对象并且未保留在数据库中,那么它将向 /projects/:project_id/todos
发出 POST
请求>。您在示例中声明要向 /projects/:project_id/todos/new
发出 POST
请求,该请求没有 POST
路线,这就是它失败的原因。
关于ruby-on-rails-3 - 为什么我的嵌套资源中出现 "no route matches [POST]"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6823883/