How to rewrite URLs using namespaces in Ruby on Rails? - ruby-on-rails

I am running Ruby on Rails 3 and I would like to set up my routes in order to rewrite URLs using namespaces.
In the routes.rb file I have:
namespace "users" do
resources :account_auth
end
So, URLs to "show an"/"create a new" account_auth page are:
http://<site_name>/users/account_auths/1
http://<site_name>/users/account_auths/new
I would like to rewrite/redirect those URLs as/to
# from 'account_auth' to 'auth'
http://<site_name>/users/auth/1
http://<site_name>/users/auth/new
How to do that? If it can be, what kinds of problems could I have?

Use the :path option:
namespace :users do
resources :account_auth, :path => "auth"
end

Related

Removing resources from url

I have a few nested resources (namespaces, pages and comments), and url looks like this:
http://example.com/namespaces/objects/pages/super-page — it's too long.
What is the best way to remove resources names from routes? I want to get something similar to this:
http://example.com/objects/super-page/
Update: there are only show action in the namespaces controller.
You can set the path attribute on the reources
resources :pages, :path => '' do
I found this article very helpful in customizing my url's http://jasoncodes.com/posts/rails-3-nested-resource-slugs
There is also a great gem for getting rid of the id's and customizing the slug, friendly_id's, http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
you can use shallow parameter in routing
e.g.:
resources :namespaces, :shallow => true do
resources :objects do
resources :pages
end
end
and then you can use routes like
page_path(1) #=> '/pages/1'
object_page_path(1,1) #=> '/object/1/pages/1'
namespace_page_path(1, 1) #=> '/namespace/1/pages/1'

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([^/;,?]+) }

How to remove controller name in REST design in Rails3?

Given a User resource, it goes like this
/user/:shortname
But how can the controller name be removed to get just
/:shortname
How can I declare this in routes.rb while keeping all CRUD functionality instant?
Updated: After reading this I'm moving to Sinatra over Rails to handle this API-like design better.
Define a custom match:
match ':shortname' => 'users#action'
Replace action in users#action with the name of the action that is supposed to receive the request. Just remember to place it in the appropriate order in your routes file. Rails looks at each line of your routes file starting at the top and selects the first matching route. ':shortname' would match any first-level path, including /users! So put it below any routes using a first-level path, which would include all of your resource routes. Here's an example:
resources :users
resources :posts
match '/blog' => 'posts#index'
match ':shortname' => 'users#action'
In routes, you should be able to do something like
resource :users, :path => '/:shortname'
Try that out and rake routes to see if that comes out as expected.

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'

How to map users in root URL in Rails 2.x routes?

My routes.rb look like this:
#...
map.resources :users
map.root :controller => "main"
#...
My app user's profiles are something like:
http://www.railsapp.com/users/3
I would like to change it to:
http://www.railsapp.com/3
Or even:
http://www.railsapp.com/username
How Rails 2.x router handles this situation?
Checkout some previous answers for 'vanity' urls:
How can I implement vanity URL's in a Rails application?
How to implement "short" nested vanity urls in rails?

Resources