rails custom route for model with parameter - ruby-on-rails-3

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

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.

Related

form_tag for create/update action rails 3

My route file looks like this:
scope :locslug/:userslug do
....
....
post 'rate/:stars' => 'articles#rate' :as => :rate_article
end
I'm trying to generate a form with an action that targets the rate action in articles. Ideally, when the form is submitted, a rating will either be created or updated. Elsewhere, I have that an article has_many ratings.
This doesn't work:
= form_tag rate_article_path, :method=>'post', :id => "rate_article" do
=hidden_field_tag :article_id, #article.id
=hidden_field_tag :stars, 0
=hiden_field_tag :user, current_user.id
Help is very much appreciated. Thank you.
does rails shows some error? I think you have your route wrong: 'rate/:stars' tells rails to expect a parameter when you call rate_article_path (something link rate_article_path(5) for 5 stars)
you should have your route:
post 'rate/:article_id' => 'articles#rate' :as => :rate_article
your form:
= form_tag rate_article_path(#article), :method=>'post', :id => "rate_article" do
=hidden_field_tag :stars, 0
now on your controller
def rate
article = Article.find(params[:article_id])
article.rates.create(:user => current_user, :stars => params[:stars])
end
(it's really simplified, you should do some validations, it's just to get the idea of what to do)

Links to resources with friendly URLs

I'm trying to write a blog in Rails 3, so I have posts. I want to make nice routes for the posts like this: posts/year/month/day/post-title. So I overrided to_param in model/post.rb and use friendly_id for title:
extend FriendlyId
friendly_id :title, :use => :slugged
def to_param
"#{year}/#{month}/#{day}/#{title.parameterize}"
end
And I added this to routes.rb:
resources :posts do
collection do
match ":year/:month/:day/:id", :action => "show"
end
member do
post 'comment'
delete 'comment/:comment_id', :action => 'destroy_comment', :as => 'destroy_comment'
end
end
In show this works:
<%= link_to image_tag("/images/edit_icon.png"),
{ :controller => 'posts', :action => 'edit' }, :id => #post %>
But in index it says
routing error:
No route matches {:action=>"edit", :controller=>"posts"}.
I'm new to Rails and I haven't managed to find out what causes this error. Can anyone help me figure out what I do wrong?

link_to different behavior from rails2 to rails3

In rails2, I was able to have code like this:
link_to(user.company.name, user.company)
which would map to:
/companies/id
but in rails 3, this same line of code throws a error stating:
undefined method `user_companies_path'
The obvious fix is to do something like:
link_to(user.company.name, company_path(user.company))
But I was wondering if anyone could explain the reason behind the change? The logic seemed a lot cleaner.
EDIT: Adding samples of my routes
In rails2, my routes looked like:
map.resources :users, :except => :edit, :member => { :details => :get }
map.resources :companies, :except => :edit, :member => { :details => :get }
In rails3, my routes are:
resources :users, :except => :edit do
member do
get :details
end
end
resources :companies, :except => :edit do
member do
get :details
end
end
The short answer is that the Rails 3 routing API bases your application on resources which is why these RESTful routes are being used, and also means that it does things like support constraints.
In Rails 2, you'd do:
resources :cars do
resource :models
member do
post :year
end
collection do
get :details
end
end
In Rails 3, you'd do:
map.resources :cars, :member => {:year => :post}, :collection => {:details => :get} do |cars|
cars.resource :model
end
You also have the :as key available which means you can then use named route helpers anywhere that url_for is available (i.e. controllers, mailers etc.)

Routing with id and handle in Rails

I'm trying to set up rails to use both the ID and the Handle (which is just an URL safe version of the title) of a blog post in the route.
match '/articles/:id/:handle', :to => 'articles#show'
resources :articles
This works, of course -- but I can't seem to set up the to_param method in the model os the longer URL -- with the handle attached, is the default.
This doesn't work (not that I really expected it to):
def to_param
"#{id}/#{handle}"
end
I get a No route matches {:action=>"edit", :controller=>"articles", error. I also tried just using the handle, but then Rails generates links to the resource just using the handle and not the ID. I know I can do it with a - in stead of a /, but I prefer the /. Any way to make this work? If I have to add some extra paremeters to my link_to helpers, that's okay.
Did you try to pass a Hash to link_to?
link_to "Link", {:id => #article.id, :handle => #article.handle}
Update
You have to modify your routes:
match '/articles/:id/:handle', :to => 'articles#show', :as => :article_with_handle
and use the following helper to generate the link:
link_to "Link", article_with_handle_path(:id => #article.id, :handle => #article.handle)
You can override the helper to simplify things:
def article_with_handle_path(article)
super(:id => article.id, :handle => article.handle)
end
and use it like this:
link_to "Link", article_with_handle_path(#article)
Okay, here's what I did to remove the query string problem from the answer above:
Changed the route to this:
match '/articles/:id/:handle' => 'articles#show', :as => :handle
Removed the to_param method from the model and then generated the link like this:
link_to 'Show', handle_path(:handle => article.handle, :id => article.id) %>
That works, but could be condensed, obviously, with the helper above. Just change the one line to: args[1] = handle_path(:id => args[1].id, :handle => args[1].handle)

Trying to create a POST request but getting No route matches [GET]

I'm trying to do something similar to Railscasts #255 but I'm getting a No Route error:
In Ryan's routes.rb file:
post "versions/:id/revert" => "versions#revert", :as => "revert_version"
In in the controller where he uses the route, versions_controller.rb
link = view_context.link_to(link_name, revert_version_path(#version.next, :redo => !params[:redo]), :method => :post)
redirect_to :back, :notice => "Undid #{#version.event}. #{link}"
In my routes.rb
post "/approve/:id" => "listings#approve", :as => "listing_approve"
and view where I use my link:
<%= link_to 'Approve Content', listing_approve_path(#listing), :method => :post %>
My tests return to me a ActionController::RoutingError: No route matches [GET] "/approve/1"
If I leave the method as a GET everything works.. Using rails 3.1.0rc5. Any guidance as to what I'm doing wrong here would be very much appreciated..
EDIT: routes.rb file (the last line is set as match right now to work)
RLR::Application.routes.draw do
root :to => "home#index"
devise_for :users, :controllers => { :registrations => "registrations" }
devise_for :users
match '/user' => "layouts#index", :as => :user_root
resources :users, :only => :show
resources :layouts, :only => [:index, :show]
resources :listings
resources :features
resources :orders
match "/preview/:id" => "listings#preview", :as => "listing_preview", :via => "get"
match "/approve/:id" => "listings#approve", :as => "listing_approve"
end
Hmmmm, it looks right to my eye. The test sounds like it is generating a GET instead of a POST though, so it might be a problem with the link_to call. You've got :method => :post there, so it should be fine. http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to seems to indicate that link_to will generate some javascript to make a POST call on click (and that users with javascript disabled will get a normal GET link unless you use :href="#"), so it might be because your test engine isn't running the javascript.
You can fix this by changing it to a button that submits a hidden form, but that might not be the visual representation you want.
It might be a precedence thing - the first matching route definition in routes.rb is used, so if you have a resources route or something like that it may be matching on that first.
I got the same problem in my rails application and I solved it the same way you did by doing a via: :get on the match instead of a via: :post. I think for some reason when you send a request in the format of /something/:id it will automatically assume its a [GET] request and search for a get route. This of course will cause problems in your routes if you have it as a :POST.
If anyone has a better solution or idea as to why you cannot send a post request in the format '/something/:id' let me know please.