Rails routing: id doesn't work - ruby-on-rails

I have the following routing rule:
match ':controller/:action/:id'
However when I use
<%= link_to "Link", :action => "some_action", :id => 10 %>
Instead of redirecting to "some_action/10" it redirects to "some_action?id=10"
How can I fix that?
P.S. I know I should be using the path methods, but is there a way to avoid them?

As Matchu said, it should work. Try making your catch-all route the first one in routes.rb. If it works then, you'll know there's another route being evaluated first.
If this doesn't work, post your complete routes.rb file.

Related

Invalid route name for GET and POST

I'm working through an older tutorial that was done for Rails 3. I'm using Rails 4.1.4.
One of the instructions is to change the routes file to include the following:
get '/boards/:board_id/conversations/:id/reply' => "conversations#reply", :as => :reply_board_conversation
post '/boards/:board_id/conversations/:id/reply' => "conversations#save_reply", :as => :reply_board_conversation
Obviously that gives me an error:
Invalid route name, already in use: 'reply_board_conversation'
It seems to me that the route is somehow trying to replicate the behaviour of a new and create action. Get for new and Post for create with a single route.
The problem is I can't figure out how to rewrite the route so it works. I've googled for solutions but can't seem to find anything. If anyone could point me in the right direction it would be hugely appreciated.
It looks like the only issue is with the duplicated "named route" name reply_board_conversation. So you could simply change one. I'd probably rename the save version to save_reply_board_conversation. Then it should work. Just remember to refer to the route this way in the future. This would primarily be used in a form tag. So, for exmaple:
<= form_tag :url => save_reply_board_conversation_path do %>
Note the use of save_reply_board_conversation_path instead of reply_board_conversation_path given that the form would be submitting a POST request instead of a GET request.
The names for these routes should be different although since the composition of the URL is the same so you really only need a name for the first one.
The trick with named routes is they generate the URL only, they do not set the HTTP request method. That has to be done independently.
That means you can call the same named route two different ways:
<%= link_to('View', board_path(#board)) %>
<%= link_to('Delete', board_path(#board), method: :delete) %>
These actually render as the same URL but one will hit the GET route, the other the DELETE one.

Why is link_to with an absolute url considered technically superior to targetting controller action?

New to rails, so if this is discussed somewhere, just link me off: I had a good search but all I could find were people trying to figure out how to use link_to, not any discussion of this comment:
link_to "Profile", profile_path(#profile)
# => Profile
in place of the older more verbose, non-resource-oriented
link_to "Profile", :controller => "profiles", :action => "show", :id => #profile
# => Profile
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
I get that the latter is more verbose, and thus undesirable, but the former seems like a strange thing to be recommending.
If I have an action at say: /blah/add and I link to it using:
link_to "Link", link_add_path
Then I'm linking to mysite.com/link/add. This is a hard coded url.
If I change the route that this maps to, I have to change every instance of link_to in my code base to point to the new absolute url. This seems crazy.
However, if I link to it using:
link_to "Link", :controller => "thing", :action => "add"
Then the url is dynamically determined. If I have to change the path all I do is edit config/routes.rb and not touch any of my code. This seems like much lower maintenance.
I appreciate it's slightly more complex than that, the blah_path variable is not actually a static route, and actually contains some smarts like the application base url and prevents you from linking to urls that don't exist, but it seems like a step backwards to facilitate a fractionally less verbose syntax.
So, what's up with that?
What technical reason would you choose the former link_to syntax over the latter?
"You're doing it wrong" :P
Seriously though: use named resources, and here's why that's cool:
Example:
you've got this in your routes file:
resources :user_orders
And you are using "user_orders_path" everywhere. Then you do a refactor and decide (because the orders are now generic) that you want the controller to be called "orders" but you don't want to break all your old code. you can do this:
resources :user_orders, controller: "orders"
And your existing links will continue to work! (plus you can add a "orders" resource to move things over to the new scheme)
There's also neat things like named links:
match 'exit' => 'sessions#destroy', :as => :logout
I'd also like to add, if you needed to refactor your controller using the old link syntax - you'd still have to change a pile of controller links!
Then I'm linking to mysite.com/link/add. This is a hard coded url.
No, it's not. link_add_path is a method generated by Rails that points to a specific route in your config/routes.rb. You can see this by running
rake routes | grep link_add
If I change the route that this maps to, I have to change every instance of link_to in my code base to point to the new absolute url. This seems crazy.
No, you don't. Take the following example
get "link/add", as: :link_add, controller: :links, action: :add
If I run the above
rake routes | grep link_add
I get
link_add GET /link/add(.:format) links#add
But what if I change the name of my controller to UrisController? Just change the route in config/routes.rb
get "link/add", as: :link_add, controller: :uris, action: :add
and now you have
link_add GET /link/add(.:format) uris#add
The link_to's don't have to change because the link_add_path method is still mapped to the newly modified line in my config/routes.rb because the route name is the same. With your more explicit way of specifying controllers/actions for every link_to, you have to go through every link and update it manually to reflect the new controller: :uris controller.
Read about Naming Routes in the rails guide.

In nested route, how is :format set to nil?

I have the following in my routes.rb:
resources :users do
resources :decisions
end
/users/new works fine, but /users/:id/decisions/new gives me:
No route matches {:controller=>"decisions", :format=>nil}
<%= link_to "New decision," new_user_decision_path(#user) %> gives me the same error.
I've looked in my rake routes and the action and the helper are listed. All of the actions listed have a (.:format) suffix option, but I don't understand how all of my DecisionsController actions are working fine without a format option except 'new'.
How is :format set to nil and what is its default?
it looks like you used a singular decision in your url when it should be decisions. Your url should be /users/1/decisions/new.
However, you should be using a named route to stop this from happening. You should see something like new_user_decision via rake routes. You can then use the new_user_decision_path for all of your links.

restful route customization

How can i customize restful route urls to friendly urls like stackoverflow ?
i have
stackoverflow.com/questions/424/
and i want to have
stackoverflow.com/questions/424/title-of-the-page
map.resources :questions
map.friendly 'questions/:id/:title', :controller => 'questions', :action => 'show'
These are my final customizations. Any better ideas ?
If there's only one controller you want to do this with, the simplest solution would probably be to add a route with an ignored parameter, like so:
# config/routes.rb
map.connect 'questions/:id/:ignored', :controller => 'questions', :action => 'show'
Make sure you put this before the default routes at the bottom of that file. Or, even better, comment them out if you're using named routes and resources (as suggested in the auto-generated comments).
StackOverflow ignores the string second parameter at request processing time. At URL construction time it adds it for humans and (probably) SEO.
I'd start by looking at the capabilities of something like friendly_id and see if those aren't what you're looking for...

Ruby on rails paths and routes

I'm trying to get the hang of basic Rails routing.
I have a model called page which I generated with a scaffold.
I have added a method called addchild which I would like to access through
'pages/addchild/:id'
So far so good. However, I want to set up a link to this method like so:
<%= link_to 'Add child page', addchild_page_path(page) %>
Passing the ID of the current page as a parameter.
When I load my index view (where the link is), I get the following message:
undefined local variable or method `addchild_page_path' for #<ActionView::Base:0xb67797d0>
Have I misunderstood how the path/link_to method works?
My routes file looks like this:
map.resources :pages
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
Any advice would be greatly appreciated.
Thanks.
You need to add a route to it to be able to use the named path methods.
Since you mentioned you used scaffolding, you probably have the route setup as a resource, so all that you need to do is add the method:
map.resources :pages, :member => {:addchild => :get}
Would give you an addchild_pages_path (and the actual created path would look like /pages/:id/addchild
You then use it like this: addchild_pages_path page, don't call the id method directly since it is not resourceful (you won't use the to_param in the page class, which you might want to do later).
If you really want the url to show up as /pages/addchild/:id (which I don't recommend) you can add
map.addchild_page "/pages/addchild/:id", :controller => :pages, :method => :addchild
before the map.resources :pages row in your routes.rb, and then use the path method as above.

Resources