Rails how to rewrite this old route map.connect? - ruby-on-rails

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.

Related

Custom url in ruby on rails

I know rails uses the controller action style urls like www.myapp.com/home/index for example
I would like to have a url like this on my rails app, www.myapp.com/my_page_here is this possible and if so how would I go about this?
You just use a get outside of any resources or namespace block in your routes.rb file:
get 'my_page_here ', :to => 'home#index'
Assuming you are using Rails 3+, do NOT use match. It can be dangerous, because if a page accepts data from a form, it should take POST requests. match would allow GET requests on an action with side-effects - which is NOT good.
Always use get, put, post or these variants where possible.
To get a path helper, try:
get 'my_page_here ', :to => 'home#index', :as => :my_page
That way, in your views, my_page_path will equal http://{domain}/my_page_here
you just need to make a routing rule to match that url
in this case it will be something like
match 'my_page_here' => 'your_controller#your_action'
your controller and action will specify the behavior of that page
so you could do
match 'my_page_here' => 'home#index'
or
get 'my_page_here', :to => 'home#index'
as suggested in other responses.
for index action in home controller if you have such a controller
see http://guides.rubyonrails.org/routing.html for more details
also see Ruby on Rails Routes - difference between get and match

Rails implement an API using Routes pointing to existing controller actions

I have a Rails app that does everything I need it to do via a HTML interface, now I'd like to bolt on an API providing access to bits of the functionality.
How would I do this selective forwarding of some API controller actions to another controller's actions using the Routes.rb?
I have tried the following:
My regular controller routes work fine using:
match 'stuff' => 'stuff#index'
get 'stuff/index'
get 'stuff/some_get_action'
post 'stuff/some_post_action'
But then when I try for my API:
match 'api' => 'api#index'
match 'api/some_get_action' => 'stuff#some_get_action', :via => :get
match 'api/some_post_action' => 'stuff#some_post_action', :via => :post
or...
match 'api' => 'api#index'
get 'api/some_get_action', :to => 'stuff#some_get_action'
post 'api/some_post_action', :to => 'stuff#some_post_action'
I get an error. When I navigate to /api/index to server a HTML page that contains forms for testing the API URLs the url_for raises a 'Routing error' exception saying 'No route matches...'.
You may want to include ':as => ' and define your route names that you may be using as your link paths.
get 'api/some_get_action' => 'stuff#some_get_action', :as => 'some_get_action'
and the link path in your index file will have 'some_get_action_path'. Not sure that 'match' or 'get' automatically resolves to a path name, which by setting ':as' it definitely will.
I like your idea for setting up this page for testing. I'm always doing it in the console, which is surely more difficult than simply clicking a link. Your links probably need to infer that they are :json requests, not :http.

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

ruby on rails - routes.rb - match file extension when multiple periods exist in filename

I have created a route plus controller for doing dynamic css in ruby on rails as per the instructions here:
http://www.misuse.org/science/2006/09/26/dynamic-css-in-ruby-on-rails/
It took some changing to account for a newer version of ruby on rails, but the problem comes in with the routes.rb entry. The original entry was this:
# dynamic CSS (stylesheets)
map.connect 'rcss/:rcssfile',
:controller => 'rcss',
:action => 'rcss'
This did not work with a newer version of RoR, and I found this solution to work:
# dynamic CSS (stylesheets)
map.connect 'rcss/:rcssfile.css',
:controller => 'rcss',
:action => 'rcss'
However, now I was bummed that I couldn't get a catch-all filetype extension handler. The request had to have the .css extension. Playing around with it further I came up with this:
# dynamic CSS (stylesheets)
map.connect 'rcss/:rcssfile.:format',
:controller => 'rcss',
:action => 'rcss'
So this is much better. Now I could potentially request a file that ended in .foobar or whatever and match it with a handler. Not that I would necessarily, but it's more about understanding everything.
So then I tried creating a file that looked something like "foo.net.rcss" . Now it would seem that the first dot messes everything up. "no routes match rcss/foo.net.css". My questions are as follows:
How can I match any filename and any extension regardless of how many dots are in the filename?
Why does the first example not work in later RoR versions?
Why do multiple dots screw up the match?
Thanks in advance for any help.
------- update -------
I am using Rails 3.0.5 . As per some more research I can shorten the syntax to:
match 'rcss/:rcssfile', :to => 'rcss#rcss'
This is the equivalent of the first example that did not seem to work, however using this syntax it works just as expected.
match 'rcss/:rcssfile:.:format', :to => 'rcss#rcss'
This also works just like my previous example #3, however it still has the problem of not matching a file with multiple periods.
It would seem that labeling a standard ":paramater" takes special consideration for the period character. ":parameter" will match a path with up to one period, ":parameter.:extension" will match a path with up to two periods, but the :extension will be only what's between the two periods, etc.
A way around this is to use what is called "Route Globbing", which uses an asterisk instead of a colon:
match 'rcss/*rcssfile', :to => 'rcss#rcss'
The only caveat is that this will match ANYTHING after the asterisk, including subdirectories. As such, you want to make sure that this does not accidentally expose any secure files or accidentally render things unintentionally.
I used this for a general case when you don't know the extension:
get '/uploads/:basename.:extension', to: 'controller#action', basename: /.*(?=\.[\w\d]+$)/
Use a regex to match the filename?
map.connect 'rcss/:rcssfile',
:controller => 'rcss',
:action => 'rcss',
:requirements => {:rcssfile => /.+\.rcss/ }
This would match (anything).rcss - you could adjust the regex for various suffixes.

Rails 2.3.5: routes names for members action to be different from the action name

I have a rails 2.3.5 application where an action acb is changed to pqr and modified so that it works only for "get" method.
To achive this I have used resource route with options like
map.resources :controller, :member => {:pqr => :get}
The original view file has acb_controller_path link in many places. If I change the path in view file as pqr_controller_path it works fine.
Is there a way I can refer acb_controller_path to controller/:id/pqr ?
You're better off changing the view paths to point to your new route, and I think I'm misunderstanding your question a little but depending which way you're trying to do it, you can try something like this I guess?
map.acb_controller '/controller/:id/pqr', :controller => "controller", :action => "pqr"

Resources