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
Related
I would like to define URLs like these ones:
/pages/VAR.1
/pages/VAR/1
So I try different configs (but none of them works):
map.page 'pages/:id', :controller => 'pages', :action => 'custom', :constraints => { :id => /[^\/]+/ }
map.page 'pages/:id', :controller => 'pages', :action => 'custom', :constraints => { :id => /(.*)+/ }
and the application redirects to 404 page. How should I do?
I'm working with ruby 1.8.7 and rais 2.3.18
Thanks in advance
Oh... my fault! I'm used with rails3, but rails2 uses "requirements" instead of "constraints".
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"
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}
This is a pretty silly question but I'm having some real trouble figuring out. I want to convert the following routes to make it compliant for Rails 3 (from 2.8.x):
map.with_options :controller => 'static_pages', :action => 'show' do |static_page|
static_page.faq 'faq', :id => 'faq'
static_page.about 'about', :id => 'about'
static_page.blog 'blog', :id => 'blog'
static_page.support 'support', :id => 'support'
static_page.privacy 'privacy', :id => 'privacy'
static_page.howitworks 'howitworks', :id => 'howitworks'
static_page.contact 'contact', :id => 'contact'
static_page.terms_and_conditions 'terms_and_conditions', :id => 'terms_and_conditions'
end
Any help would be much appreciated!
I think I would do it like this:
scope '/static_pages', :name_prefix => 'static_page', :to => 'static_pages#show' do
for page in %w{ faq about blog support privacy howitworks contact terms_and_conditions }
match page, :id => page
end
end
This is awesome, I just wrote an article about this a couple weeks ago:
Routing in Ruby on Rails 3
It goes over most aspects of the conversion, with a downloadable sample app. While I didn't cover the with_options conversion specifically, I can do a little of that here. Here's a short way:
scope :static_pages, :name_prefix => "static_page" do
match "/:action", :as => "action"
end
This matches all the routes you have above, and your named routes would look like this:
static_page_path(:faq)
static_page_path(:about)
...and so on. If you want your named routes to still look like static_page_faq_path then you can specify them one at at time, like so:
scope '/static_pages', :name_prefix => 'static_page' do
match '/faq', :to => 'static_pages#faq'
match '/about', :to => 'static_pages#about'
# fill in all the rest here
end
I hope this helps!
How can I make Twitter-style routes with Rails3?
I've tried the following:
match ':username', :controller => "users", :action => "show"
match ':username/:controller(/:action(/:id))', :path_prefix => '/:username'
EDIT
After some more digging through the docs, I did this and it seems to work:
scope '/:username' do
resources :clubs
end
What is the "scope" method exactly and is there an automatic way of generating link_to URLs in my views?
The following matcher will match /dhh/update/1
match ':username/update/:id' => 'updates#show'
'update#show' is new in Rails 3 and is the short version of :controller => 'updates', :action => 'show'
Try clubs_path(:username => 'bob').