Rails2 to Rails3 routing conversion - ruby-on-rails

Routing makes zero sense to me, is there an easy way to convert this:
#map.resources :bicycles, :controller => 'store/bicycles', :path_prefix => 'store'
thanks in advance

The controller option is also available in Rails 3 and the path_prefix can be achieved with scope. The below is the conversion of the route to Rails 3
scope 'store' do
resources :bicycles, :controller => 'store/bicycles'
end

Related

How to translate root specific resource declaration from Rails 2 to Rails 3?

I'm migrating an application from Rails 2.3.5 to Rails 3.2.8.
I have this route from the Rails 2 app that is giving me headaches:
map.resources :soumission_vt,
:path_prefix => "/soumission/VT/:page_id", :as => 'police/:action/:id',
:requirements => {:page_id => /\S+/}
wich generates the following:
soumission_vt_index GET /soumission/VT/:page_id/police/:action/:id(.:format) {:controller=>"soumission_vt"}
POST /soumission/VT/:page_id/police/:action/:id(.:format) {:controller=>"soumission_vt"}
new_soumission_vt GET /soumission/VT/:page_id/police/:action/:id/new(.:format) {:controller=>"soumission_vt"}
edit_soumission_vt GET /soumission/VT/:page_id/police/:action/:id/:id/edit(.:format) {:controller=>"soumission_vt"}
soumission_vt GET /soumission/VT/:page_id/police/:action/:id/:id(.:format) {:controller=>"soumission_vt"}
PUT /soumission/VT/:page_id/police/:action/:id/:id(.:format) {:controller=>"soumission_vt"}
DELETE /soumission/VT/:page_id/police/:action/:id/:id(.:format) {:controller=>"soumission_vt"}
I translated it this way in Rails 3:
scope '/soumission/VT/:page_id', :constraints => {:page_id => /\S+/} do
resources :soumission_vt, :as => 'police/:action/:id'
end
but I am getting an Invalid route name: 'police/:action/:id_index'...
So is there a way to reproduce those routes in Rails 3?
Thanks!
After one try, I succeeded to make it work with the following lines of code:
scope '/soumission/VT/:page_id' do
get 'police/new', controller: :soumission_vt, action: :new, as: :new_soumission_vt
end
Hope this will help you finish your migration in time !
;)

How can I rename a Rails controller with a route?

I have a controller in a Rails 3 app named "my_store." I would like to be able to use this controller as is, except replacing "my_store" in all the URL's with another name. I do not want to rename the controller file, and all the references to it. Is there a clean way to do this with just a routing statement?
If you use RESTful routes:
resources :another_name, :controller => "my_store"
Otherwise:
match "another_name" => "my_store"
If your routes are RESTful, this is pretty easy.
resources :photos, :controller => "images"
You can see how to do this and other helpful Rails routing information in the Rails routing guide.
Update, the other guys are correct, to replace all references you would change the resources name and corresponding controller in routes.rb! My answer is only good to set a specific route.
Yup, you would do this in your routes.rb using the :as option to specify
example:
match 'exit' => 'sessions#destroy', :as => :logout
source

Routing: prefix item name before controller resources

I am trying to get the following routes to work in my Rails 3 app.
Scenario:
I have the following controllers in my app:
Practices
Doctors
Patients
Prescriptions
Putting up :resources for each of them in routes.rb gives me routes from
example.com/practices
example.com/doctors/1/edit etc
What I'd like to have however is the following resourceful routes, for example:
example.com/james_practice/docs that translates to the doctors controller
example.com/james_practice/awesome_prescriptions that routes to the prescriptions controller etc. etc.
both of these giving me access to :practice name and furthermore route the correct controller with all the helpers like edit_docs_path(doctor) etc.
How would I go about this? I've used
resources :prescriptions, :path => "/:practice/awesome_prescriptions"
but although it showed the correct routes in "rake routes", it still wouldn't work as expected.
I think this is the route you're looking for:
scope :path => ":practice" do
resources :docs, :controller => "doctors"
resources :awesome_prescriptions, :controller => "prescriptions"
end
By the way, you didn't give me the example of Patients, so I didn't put it there.
map.resources is just a pattern, more like a shortcut. If you want to have URLs like in your examples you should use "named routes". A good screencast for Rails 2.x: http://railscasts.com/episodes/34-named-routes
Edit: Read section 3.2 in Rails Tutorial: http://guides.rubyonrails.org/routing.html
p.s. Also you'll face a case where people use "." in their names and it'll cause problems.
Here's my route for "tags"
map.resources :tags, :requirements => { :id => %r([^/;,?]+) }

Using Dashes for urls in ruby on rails

I have a view folder product_types. The name of the controller file is product_types_controller and the class I have is ProductTypesController. I'd normally keep the _ format that rails prefers but I need to keep the current page syntax for search index reasons.
How do I get this controller to show up for mysite.com/product-types and all pages in the folder product_types to appear for mysite.com/product-types/some-page? Do I need to name the pages with - or should I use the _ syntax as well and just change the routes.
This is for a Rails 2.3.8 site.
Thanks
For Rails 3 you have to do this differently:
resources "product-types", :as => :product_types, :controller => :product_types
In case you're using namespaces in your routes in Rails 3, you can use the following for dashes in urls:
namespace :product_types, :path => "product-types" do
If you're using RESTful routes, you can do this:
map.resources :product_types, :as => 'product-types'
I hope this helps!
In rails 3 you can do:
resources :product_types, :path => '/product-types'

Routing resources/paths with :path_prefix and :name_prefix

I have the following route definied:
map.resources :addresses, :path_prefix => ':site', :name_prefix => 's_'
I've had no problem correcting my scaffolding links for "Show" and "New". But I am getting a failure to generate error when attempting to use:
edit_s_address_path(address) or edit_s_address
rake routes shows that this is the proper path. I'm perplexed. Thanks in advance.
Shouldn't you use s_edit_address_path(address)? According to the Rails Guide on routing, the name_prefix comes at the start of the route name.

Resources