0

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

我想将“创建新评论”的 URL 定义为,/reviews/new/1234即将创建的应该引用的 URL 在哪里。1234book_idReview

我的路线(为简洁起见)定义为:

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
4

1 回答 1

1

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

文档在这里用于使用 as

于 2013-11-06T22:27:26.897 回答