ruby-on-rails-3 - 为什么我的嵌套资源中出现 "no route matches [POST]"?

标签 ruby-on-rails-3 resources routes

我有一个项目,其中包含具有待办事项任务项目。当我尝试创建新任务时,在提交时收到此错误:

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/

相关文章:

ruby-on-rails - rails,如何从 url 字符串中获取参数?

ruby-on-rails - sql执行后得​​到切片结果

javascript - Vue-router 仅在添加全局导航守卫后抛出 "Cannot read property ' matched' of undefined"错误

javascript - Angular ui-router 在路径参数后扩展 url

grails - 如何将Google Web字体CSS添加到Grails资源

ruby-on-rails - 防止应用程序路由阻塞 Rails 3 中的插件路由

ruby-on-rails-3 - 如何从 mina 部署脚本运行自定义 rake 任务

ruby-on-rails-3 - 环境与 WickedPDF 和 Heroku 冲突

c# - 以不同的语言运行我的 C# 应用程序

java - 如何将直接访问的资源打包成jar文件