Rails routes - index page for subdomain and standard url - ruby-on-rails

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.

Related

How to route 2 different URL's to the same action in Rails 2?

I'm dealing with an application that's using Rails 2.3.18 and I'm wondering if it's possible to route multiple url's to the same action without creating a new map.
map.home '/', :controller => 'pages', :action=>'home', :path=>'home'
Is it possible to route to '/', as well as '/home' in this mapping?
I've been having a look through the routing documentation and I can't seem to find anything that allows you to do something like this, is it possible?
you can try this:
map.home '/', :controller => 'pages', :action => 'home'
map.home '/home', :controller => 'pages', :action => 'home'
Yes, you can do these routes as :
root "page#home" // this work as root for '/' path
&
get "/home" => "page#home"
This is the solution but if you want to do it with seperate routes without root_path then :
get "/" => "page#home"
get "/home" => "page#home"

Rails route: pull entire path string into one parameter

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 :)

Why to add a connection in routes file when using link_to in rails 2

I was trying to accomplish the following:
<%= link_to "Log out", { :controller
=> 'users', :action => 'logout' }, :class => 'menulink2' %>
But it didn't work, it always redirected me to a show view. I had to had the following to my routes.rb:
map.connect 'users/logout',
:controller => 'users', :action =>
'logout'
Why didn't rails recognize the action I was passing ('logout') ?
That logic has to be specified somewhere. There's got to be some mapping from the hash {:controller => 'users', :action => 'logout'} to a url, and the place that's done in rails is the routes.rb file. In older versions of rails many routes.rb came with a default at the end:
map.connect ':controller(/:action/(:id(.:format)))'
Which would make it so that most any :controller, :action hash could be specified and then routed to host.url/:controller/:action.
With the more modern versions resource-based routes are heavily favored, and controllers which don't follow rails' REST conventions (i.e. having only :index,:show,:create,:new,:edit,:update,:destroy methods) generally have to have their routes explicitly specified in some way.
(Either with map.resources :users, :collection => {:get => :logout} or with map.connect( 'some_url', :controller => 'users', :action => 'logout'}))
I'm guessing, but the reason they did that is probably that the actions of a controller are really just its public methods.
It's frequently nice to have public methods in your controllers that aren't url-end-points for testing purposes.
For instance, you could have before_filters as public methods that you might want to test without having to use #controller.send(:your_before_filter_method) in your test code.
So they whitelist the resource actions, and make the others unreachable by default. Let me look through the rails changelog and see if I'm right.

Querystrings in rails routes, how to manage their order?

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 :)

Get SEO friendly URLS with Rails without method_missing?

Currently we are using method_missing to catch for calls to SEO friendly actions in our controllers rather than creating actions for every conceivable value for a variable. What we want are URLS like this:
/students/BobSmith
and NOT /students/show/342
IS there a cleaner solution than method_missing?
Thank you!
You can define a route for that particular format fairly easily.
map.connect "/students/:name", :controller => :students, :action => :show, :requirements => {:name => /[A-Z][A-Z]+/}
Then in your show action you can find by name using params[:name].
You can create a catch-all route. Put this at the bottom of config/routes.rb with whatever controller and action you want:
map.connect '*path', :controller => '...', :action => '...'
The segments of the route will be available to your controller in the params[:path] array.

Resources