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 :)
Related
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.
In my Rails 2.3.11 app, I want to specify that the default format for a route is :xml. According to the documentation I can do this using :defaults
map.connect '/myroute', :controller => 'mycontroller',
:action => 'myaction',
:defaults => {:format => :xml}
The documentation specifically says this should work:
You can also define other defaults in a route by supplying a hash for
the :defaults option. This even applies to parameters that are not
explicitly defined elsewhere in the route.
But if I do that, then I get this error:
/Users/simon/myproject/vendor/rails/actionpack/lib/action_controller/routing/builder.rb:107:in `assign_route_options':
format: No matching segment exists; cannot assign default (ArgumentError)
I see that a lighthouse ticket has been raised about this; a respondent notes that it works for resources but not named routes; an admin has incorrectly marked it as fixed because he's tested it on resources. Ho hum.
Elsewhere it is suggested that i do it like this:
map.connect '/myroute', :controller => 'mycontroller',
:action => 'myaction',
:format => :xml
but then if I test it
assert_generates '/myroute', :controller => 'mycontroller',
:action => 'myaction'
I get told that no route matches :controller => 'mycontroller', :action => 'myaction' - I have to put the format in by hand, so it isn't a default.
How do I specify a default in a rails 2.3 route? Do I need to get them to reopen the ticket and actually fix the bug? Is there any hope that that will happen now Rails 3 is out?
Hmmm that is pretty weird. I've used :defaults hash in a named route, and it worked for me. Can you try using a named route instead and see if it works ?
map.myroute '/myroute', :controller => 'mycontroller',
:action => 'myaction',
:defaults => {:format => :xml}
I'm trying to do something trivial. I have a bunch of URLs that I need to map like the following:
http://example.com/foo
http://example.com/foo/something
Both need to go to the same controller/action. The problem I'm having is when http://example.com/foo is invoked, I need to specify a default query parameter. I thought that's what the :defaults hash does in routes.rb, but unfortunately the following doesn't work:
map.connect 'foo', :controller => 'something', :action => 'anaction',
:defaults => { :myparam => 'foobar' }
This should route http://example.com/foo to the something controller, anaction action, and make params[:myparam] point to the string "foobar".
I'm assuming for the second example http://example.com/foo/something, I'll need an additional route.
What's the best way to tackle this?
I wouldn't complicate things by adding such logic to my routes file, I'd just do it in my action:
params[:my_param] ||= 'foobar'
Untested, but:
map.connect 'foo', :controller => 'something', :action => 'anaction', :myparam => 'foobar'
It looks like the :controller and :action arguments in there are not in any way special, but just end up feeding into params. The 2.3.8 documentation seems to confirm this.
More formally, you can include
arbitrary parameters in the route,
thus:
map.connect ':controller/:action/:id', :action => 'show', :page => 'Dashboard'
This will
pass the :page parameter to all
incoming requests that match this
route.
I have been using the following route successfully in my Rails 2.x application:
map.user ':id', :controller => 'users', :action => 'show'
This, as my lowest route, properly catches things like /tsmango and renders Users#show.
I'm now trying to add a second, similar route like:
map.post '~:id', :controller => 'posts', :action => 'show'
Because neither my users or my posts are allowed to contain ~ and because this route will appear above my map.user route, I assumed this would properly catch any call starting with /~ and render my Posts#show action. Unfortunately, I'm having trouble getting this one to work.
What's interesting is that this similar route works perfectly:
map.post ':id~', :controller => 'posts', :action => 'show'
Although, I'm certainly willing to go with ':id~' since it has the same result, at this point I'm really just frustrated and curious as to how you would build a route that matches '~:id'.
It's worth mentioning that I do not want to modify my to_param method or my actual user and post slugs to include the prepended ~. I just want that in a route to indicate which action should handle it. Unless I'm mistaken, this rules out the use of something like:
:requirements => {:id => /\~[a-zA-Z0-9]/}
Thanks, in advance, for any help you can provide!
Update: I'm aware of route priority and stated above that I am placing the '~:id' route above the ':id' route. I receive the following error while trying to generate the url like post_path(#post):
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.to_sym
Routes are prioritized depending of the order in which they're declared.
When you define first the :id route, the second one is never executed.
In order for this to work, you just have to first define the ~:id route and then the :id one.
map.post '~:id', :controller => 'posts', :action => 'show'
map.post ':id', :controller => 'users', :action => 'show'
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.