rails带有参数的模型自定义路径

时间:2013-11-06 22:10:20

标签: ruby-on-rails-3 routes

我无法为一个模型定义Rails的“新”路径,该模型将一个参数传递给它将链接到的另一个模型。我们有遗留的URL结构,不幸的是 嵌套的resource路由在这里不起作用。

我想将“创建新评价”网址定义为/reviews/new/1234,其中1234是即将创建的book_id Review get '/reviews/:book_id' => 'reviews#index', :as => 'reviews_path' get '/reviews/new/:book_id', :to => 'reviews#new', :as => 'new_review_path' post '/reviews/:book_id' => 'reviews#create' get '/reviews/:book_id/:id' => 'reviews#show' get '/reviews/:book_id/:id/edit' => 'reviews#edit', :as => 'edit_review_path' delete '/reviews/:book_id/:id' => 'reviews#destroy' 应该参考。

我的路线(为简洁而剪断)定义为:

rake routes | grep review

reviews_path GET /reviews/:book_id(.:format) reviews#index new_review_path GET /reviews/new/:book_id(.:format) reviews#new POST /reviews/:book_id(.:format) reviews#create GET /reviews/:book_id/:id(.:format) reviews#show edit_review_path GET /reviews/:book_id/:id/edit(.:format) reviews#edit DELETE /reviews/:book_id/:id(.:format) reviews#destroy 返回:

<%= link_to 'new review', new_review_path(book_id: @book.id) %>

在我的视图模板中,我有:

ActionView::Template::Error (undefined method `new_review_path' for #<#<Class:0x007f818f7117c8>:0x007f818f70e208>):

失败了:

Review

为了完整性,我的class Review < ActiveRecord::Base attr_accessible :book_id, :title, :content, :tags belongs_to :book end 模型如下:

{{1}}

1 个答案:

答案 0 :(得分:1)

从路线_path条件的末尾移除:as。现在它正在寻找new_review_path_path

文档is here for using as