How do I specify a default in a rails 2.3 route? - ruby-on-rails

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}

Related

Changing Rails 2 Routes to Rails 3 Routes

Hi I'm trying to change my routes from a rails 2 project to match the syntax in rails 3. How would I change this:
map.connect "/stylesheets/:action.css",
:controller => "stylesheets",
:format => "css"
so that the :action can be matched to an action from the stylesheets controller?
Would it be something like this?
match 'stylesheets/:action.css', :to => "stylesheets#{:action}"
Also, what is the syntax for :format in the routes for rails 3?
You're almost there with your solution. The only thing you have to change is the way you reference :action in the :to value.
match 'stylesheets/:action.css', :to => 'stylesheets#:action', :format => :css
As you can see, the syntax for :format didn't change.
I think it would be smth like:
match 'stylesheets/:action', :controller => :stylesheets, :defaults => { :format => :css }
or you can constraint your routes to .css format using :constraints => { :format => 'json' }
I recommend you to read through http://guides.rubyonrails.org/routing.html

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.

Rails 2 routes to rails 3 conversion

does anyone know if there is a rails routes converter online? I was not able to find one. I am trying to convert this line:
map.add_payment_profile 'add_payment_profile/:id', :controller => 'payment_profile_controller', :action => 'add_payment_profile'
Thanks!! So if I understand correctly:
map.create_cim_payment_profile 'create_cim_payment_profile_user', :controller => 'authorize_net', :action => 'create_cim_payment_profile', :only => :post would be
match 'create_cim_payment_profile_user' => 'payment_profile#create_cim_payment_profile', :as => :create_cim_payment_profile
I believe what you're looking for is
match 'add_payment_profile/:id' => 'payment_profile#add_payment_profile',
:as => :add_payment_profile
Rails 3 has a new controller#action shorthand for mapping controller actions. You will also need to specify :as to created a named route.
may be this will work:
match 'add_payment_profile/:id',:controller => "payment_profile_controller",
:action => "add_payment_profile"

Creating a Custom Rails Route

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.

Resources