Reputation: 31064
I'm having trouble defining a Rails 'new' route for a model that takes a param to another model to which it will be linked. We have a legacy URL structure in place, so unfortunately
a nested resource
route won't work here.
I'd like to define the "create a new review" URL as /reviews/new/1234
, where 1234
is the book_id
that the soon-to-be-created Review
should reference.
My routes (snipped for brevity) are defined as:
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
returns:
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
In my view template I have:
<%= link_to 'new review', new_review_path(book_id: @book.id) %>
which fails with:
ActionView::Template::Error (undefined method `new_review_path' for #<#<Class:0x007f818f7117c8>:0x007f818f70e208>):
For completeness, my Review
model looks like:
class Review < ActiveRecord::Base
attr_accessible :book_id, :title, :content, :tags
belongs_to :book
end
Upvotes: 0
Views: 80
Reputation: 13344
Remove the _path
from the end of the :as
conditions on your routes. Right now it's looking for new_review_path_path
.
Documentation is here for using as.
Upvotes: 1