I’m upgrading my application from Rails 2.3.11 to 3.1.1. After
updating my routes file with the new syntax, I’m seeing a change in
behavior of the link_to method when passing an ActiveRecord object as
the link destination.
Here is an example of an old (non-RESTful) route from my 2.3
application’s routes.rb:
map.with_options :controller => 'widget' do |widget|
widget.widget 'widget/show/:id', :action => 'show'
end
Here is what this route became in my 3.1 file:
controller :widget do
scope "widget" do
match "show/:id", :action => "show", :as => :widget
end
end
The output of rake routes
is similar. 2.3:
widget /widget/show(/:id)
{:controller=>“widget”, :action=>“show”}
3.1:
widget /widget/show/:id(.:format)
{:action=>“show”, :controller=>“widget”}
What’s different is that now my link_to calls that look like this:
link_to(“Text”, @widget_object)
Produce this error message:
“Routing Error: No route matches
{ :controller=>“widget”, :action=>“show”, :id=>#<Widget id:
123, …> }”
After seeing this passage in the Rails Guides:
“<%= link_to “Magazine details”, @magazine %>…This allows you to
treat instances of your models as URLs, and is a key advantage to
using the resourceful style.”
–Rails Routing from the Outside In — Ruby on Rails Guides
from-objects
my guess is that Rails 2.3 happily handled my link_tos because my
named route was widget_path/widget_url (changing the name would cause
my 2.3 app’s link_tos to break as well), but Rails 3 insists on
RESTful/resourceful routes if you’re going to use this link_to style.
Is that right? I’ve started refactoring and adding the necessary
RESTful routes to make my link_tos work again with good results
but I want to confirm that I really understand the problem.
William