How to map users in root URL in Rails 2.x routes? - ruby-on-rails

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?

Related

Redirection in Rails 4 routes

My site used to have a mobile view here:
https://www.example.com/m/home
We have deprecated the mobile views and now I need a simple way to trim the /m/ off the URL so that the request proceeds to the correct page.
Example:
https://www.example.com/m/about => https://www.example.com/about
https://www.example.com/m/user/:id => https://www.example.com/user/:id
I'm hoping to solve this in the Rails routing without having to introduce a new controller action or meddle with nginx. I have 100+ routes. Thanks in advance.
Rails version: 4.2
There is a redirection module (also documented in the guide).
Something like :
get '/m/about', to: redirect('/about')
get '/m/user/:id', to: redirect('/user/%{id}')
Which you can combine with route globbing for a generic solution :
get '/m/*path', to: redirect('/%{path}')
How about just refactor your routes a bit:
Eg: Previous routes.rb
resources :users
# ...
Now, it becomes:
['m', ''].each do |sc|
scope sc do
resources :users
# ...
end
end

How to rewrite URLs using namespaces in 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

Short-hand routes and redirections with Ruby on Rails

I have some resources in my routes file:
resources :snippets
resources :pastes
Users can access a snippet like this:
/snippets/72384
/pastes/ac6Xs28
As a short-hand, for tweets, IRC, etc… I want /s/<id> to be redirected to /snippets/<id>, and /p/<id> to /pastes/<id>. I want this redirection to use the Location HTTP header and the 301 Moved Permanently HTTP status code.
How am I about to do this? Thanks.
I'm using Rails 3 with WEBrick
You can do it in routes.rb
match "/s/:id", :to => redirect("/snippets/%{id}")
Here's a nice overview of routing in Rails3.

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 non-English controllers in rails3

I have to following problem:
If i want to follow the Rails naming convention i have to use the plural version of the model's name as my controller name.
Example:
rails g scaffold_controller Content
In this case i have a model(Content) and the previous command is going to generate a controller with the name Contents.
So what if I have a German website and I would like to use site.tld/inhalt/something-something instead of site.tld/contents/something-something.
I was thinking about two solutions:
a, use the German version of the word when I am generating the controller(afaik it is not supported by the scaffold_controller generator
b, generate the whole site(models, controllers) in English and route the specific request to a certain controller with named routes like:
match 'logout', :to => 'sessions#destroy'
In this case I can't use RESTful routes like:
resources :products
What do you think?
You can use routing configuration to change the url that matches this controller:
resources :contents, :path_names => { :new => 'neue', :edit => 'bearbeiten' }, :path => 'inhalt'
The contents_path method in your view will still work, but it'll output /inhalt as required and that will match. The /contents path won't match though. So if you call edit_content_path(#content) you'll get something like /inhalt/1/bearbeiten.
See Rails routing guide for more details.

Resources