How can I rename a Rails controller with a route? - ruby-on-rails

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

Related

Change paths for link_to missing out the model name

I've managed to get my routes set up (with help from these questions Routing without the model name and Permalinks with Ruby on Rails (dynamic routes)) so that articles can be accessed via my-domain/permalink rather than my-domain/articles/permalink or, the original my-domain/articles/id
Now I would like to make the paths that the link_to helper gives point to /permalink rather than /articles/permalink. I've looked at http://guides.rubyonrails.org/routing.html#overriding-the-named-helpers and see how I could redirect to eg. /images/permalink, but can't see how to have no model name present.
Can anyone suggest a way to do this?
Using :as on a match ... line in your routes file will make this work (it operates a little differently from using :as on a resources ... line):
match '/:id' => 'articles#show', :as => "article_permalink", :via => 'get'
Then you can do:
link_to "Show", article_permalink_path(article)
See Naming Routes in the Rails Guides

Rails how to rewrite this old route map.connect?

How do I rewrite this old route Rails 1.2.6 to Rails 3? :
# Allow downloading Web Service WSDL as a file with an extension
# instead of a file named 'wsdl'
map.connect ':controller/service.wsdl', :action => 'wsdl'
I can´t see how I should use match route etc.
I have used:
match ':controller/service.wsdl', :action => 'wsdl'
But I dont think it is working correct
Try this:
match '/controller/service.wsdl' => 'controller#service.wsdl', :as => :wsdl
I'm guessing that your controller is not named controller. If it is, I'd rename it and change the above route as well.
I haven't found a good solution to converting Rails 2 parameterized :controller and :action generic routes to the more explicit Rails 3+ format. What I've had to do is go through every permutation in my app and add an explicit route for everything I needed to support. For example, in your case, if you had 3 controllers that supported the wsdl action, I'd add a new route for each using either match or get.
Here's what it might look like, assuming you had a foo_controller, bar_controller, and a blah_controller that all support the wsdl action:
get '/foo/service.wsdl' :to => 'foo#wsdl'
get '/bar/service.wsdl' :to => 'bar#wsdl'
get '/blah/service.wsdl' :to => 'blah#wsdl'
This gets even more fun when you need to support every action on a controller when they use :action.
If anyone has a better method, I'm open (and eager) to hear of a better way.

Question about routes.rb

Rails newbie here.
Can anyone please explain the difference to me between the following lines of code:
match '/' => 'posts#index'
and
match '/' => 'posts#index', :as => 'posts'
The reason I'm asking is because when I use the latter code, I cannot create new posts :|
The latter is creating a named route. It creates a helper that you can call from your views, in this case, posts_path & posts_url.
That being said, I'm not sure how you are able to create new posts with either of those as you are not defining the posts#new or posts#create. Is there more to your routes file than these? Also, I'm not sure if it's a requirement or not, but you should pass your :as option as a symbol, so :as => :posts.
For reference, you can run rake routes from console and see a list of all the routes that are defined in your application. You'll also see how they are named—that's the column all the way to the right—which you can then append _path or _url to.

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.

Resources