So, I'm a bit new to rails routing, especially with querystrings.
I'm looking to create a URL that looks like this /dashboard/view_mode/2010/11/18. I also have the need for /dashboard/2010/11/18 and /dashboard/view_mode
Dashboard is a controller, the rest are parameters. I have this relevant lines in my routes.rb
map.connect 'dashboard/:view_mode/:year/:month/:day', :controller => "dashboard", :action => "switch_view"
map.connect 'dashboard/:year/:month/:day', :controller => "dashboard", :action => "index"
map.connect 'dashboard', :controller => "dashboard", :action => "index"
map.connect 'dashboard/:view_mode', :controller => "dashboard", :action => "switch_view"
map.dashboard 'dashboard/:view_mode', :controller => "dashboard", :action => "index"
Where I'm running into an issue is generating this /dashboard/view_mode/2010/11/18 from a starting point of this /dashboard/2010/11/18.
I end up with /dashboard/view_mode/2010/11/18?view_mode=my_view_mode which doesn't work.
Seems like this should be simply, but ... erg, I am just not getting it after trying for awhile.
Thanks.
If anyone comes across this, I ended up moving my view_mode param to the end of the URL ('dashboard/:year/:month/:day/:view_mode'), which removed the need for another solution, and also seems to make more sense overall anyway :)
Related
I've just added a CMS to my rails 2.2.2 app. I want to have it set up so that at the bottom of my routes i have a catch-all which shoves the entire path into a single parameter and then calls the cms controller, which then looks for a page matching that path
eg
http://mysite.com/something/about/foo
=> {:controller => "cms", :action => "show", :page => "something/about/foo"}
I can't figure out what options i need to add (if any) to stop it splitting on the slashes. Any ideas anyone? Remember this is rails 2. Thanks!
Just discovered the answer to this in the official rails api documentation (doh):
4.9 Route Globbing
Route globbing is a way to specify that a particular parameter should be matched
to all the remaining parts of a route. For example
map.connect 'photo/*other', :controller => 'photos', :action => 'unknown',
In my case:
map.connect "/*page", :controller => "cms", :action => "show"
means that
http://mysite.com/something/about/foo
=> {:controller => "cms", :action => "show", :page => ["something", "about", "foo"]}
which is fine as i can easily then join params[:page] to get the full path again.
thanks for reading :)
If I have a controller, how do I access it via URL with newly added methods?
Reason I am confused is because I have a route,
map.connect 'assignments/:external_id.:format', :controller => "assignments", :action => "show", :external_id => /\d{6}/
It seems that I can't access any other method within the assignments controller because if i do
mysite.com/assignments/other_method
It'll assume that other_method is an ID I'm passing into the show controller, as specified in the route entry above.
Edit:
I added this to the top:
map.connect 'assignments/send/', :controller => "assignments", :action => "send"
and am now getting this error:
ArgumentError in AssignmentsController#show
The route for assignments/send is the first declration for any of the assignments controller
Your routing table should have it in this order
map.connect 'assignments/:external_id.:format', :controller => "assignments", :action => "show", :external_id => /\d{6}/
map.connect 'assignments/send/', :controller => "assignments", :action => "send"
to end with
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
as your most general case.
Just specify the right route pattern for that second case and make sure you keep in mind that the mappings are evaluated from top to bottom (first match gets executed).
In Ruby on Rails, how can I do the equivalent of this in a more elegant routes line? I may have to add many of these...
map.connect '/about', :controller => "site", :action => "about"
map.connect '/contact', :controller => "site", :action => "contact"
map.connect '/preview', :controller => "site", :action => "preview"
Thanks!
You can do this:
map.connect '/:action', :controller => "site", :action => /about|contact|preview/
The part :action => /about|contact|preview/ makes sure that only the listed words can be used as action in this route,.
But do not forget to move the route suggested by #Tomas to the bottom of your routes.rb
Otherwise it will catch routes that should not be caught.
I am attempting to create a custom route in rails and am not sure if I am going about it in the right way. Firstly I have a RESTful resource for stashes that redirects to mystash as the controller:
map.resources :stashes, :as => 'mystash'
site.com/mystash goes to :controller => 'stashes', :action => 'show'
Which is what I want. Now is where it gets somewhat confusing. I would like to be able to add conditional params to this route. Ultimately I would like to have a route that looks like this:
site.com/mystash/zoomout/new/quiz_on/
I have places this in routes:
map.connect 'mystash/:zoom/:nav_option/:quiz',
:controller => 'stashes',
:action => 'show'
map.connect 'mystash/:zoom/:nav_option',
:controller => 'stashes',
:action => 'show'
map.connect 'mystash/:zoom',
:controller => 'stashes',
:action => 'show'
map.connect 'mystash',
:controller => 'stashes',
:action => 'show'
My routes have ended up looking like this in the browser:
site.com//mystash/zoomin?nav_option=New&quiz=quizon
and this is what one of my links looks like:
<%= link_to "In", stash_path("zoomin", :nav_option => #nav_option, :quiz => #quiz) %>
Any help is appreciated, I am pretty new to custom routes!
You should be giving these routes different names instead of the default, or you should be specifying your route with a hash and not a X_path call. For instance:
map.stash_zoom_nav_quiz 'mystash/:zoom/:nav_option/:quiz',
:controller => 'stashes',
:action => 'show'
map.stash_zoom_nav 'mystash/:zoom/:nav_option',
:controller => 'stashes',
:action => 'show'
Keep in mind that when you declare a named route, the parameters in the path must be specified in the X_path call with no omissions, and not as a hash.
link_to('Foo', stash_zoom_nav_quiz_path(#zoom, #nav_option, #quiz))
link_to('Bar', stash_zoom_nav_path(#zoom, #nav_option))
The alternative is to not bother with named routes and let the routing engine figure it out on its own:
link_to('Foo', :controller => 'stashes', :action => 'show', :zoom => #zoom, :nav_option => #nav_option, :quiz => #quiz)
If you're uncertain what routes are defined, or how to call them, always inspect the output of "rake routes" very carefully. You can also write functional tests for routes with the assert_routing method.
I am using the account_location plugin in my rails app and have the following route
map.root :controller => "dashboard", :action => 'index'
This route match's on both subdomain.domain.com and www.domain.com
How can I set it up so these two domains match different routes?
Thanks,
Andy
This is kind of an indirect answer, but you might want to look into using subdomain_fu. If you use subdomain_fu you'll be able to change that route.
For example:
map.root :controller => "dashboard", :action => 'index', :conditions => { :subdomain => false }
would not match requests with a subdomain.
Here's a great Railscast if you're interested.