I am using Ruby on Rails 3.0.9 and I had this problem: Trouble on setting the ':controller' parameter for a 'link_to' statement. No one answered with a solution but, trying and re-trying to find and solve the problem, I have found that on passing "old style" parameters (that is, using controller
and action
options instead of named route paths) sometime a link_to
doesn't work as expected (that is, you get a ActionView::Template::Error - No route matches
error although if you have controller and routes properly stated). Anyway you can set for that something like the following:
<%= link_to("New article", {:controller => '../', :action => 'new'}) %> # Note the "'../'"
and it works.
I would like to know when and why, in general, it happens: the controller
option seems "forced" to refer to the relative or absolute path and not to the controller parameter you passed in.
That is, if I set :controller => articles
and I use the above code in a view file managed by the controllers/article/categories_controller.rb
I get the following error:
`ActionView::Template::Error (No route matches {:controller=>"articles/categories/articles", :action=>"new"})`
because, as said above, the controller seems to refer to the /articles/categories
path instead of /articles
path (as it should be since, in this example, I set the controller to articles
). Can be it a router problem?