Rails Routing: Set specific controller action to a different path

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Rails Routing: Set specific controller action to a different path



I am looking to set the show action for a controller to a specific path (a path without the controller prefix). I know this can be done by controller by doing this


show


resources :items, path: ''



But is there a way to do this on only one specific action within the controller?



My end goal is to be able to say www.example.com/my-item-name and take the user to the item without changing the URL. I tried using a catchall route but redirecting adds the prefix back which I do not want.


www.example.com/my-item-name



Any ideas?




2 Answers
2



You can specify which controller and action respond to a certain route in your routes.rb file, like this:


routes.rb


get 'something', to: 'controller_name#action_name'



See Rails Routing Guide.



You can define single routes manually with the match, get,post, put, macros:


match


get


post


put


get :bar, to: 'foos#bar'
get :bar, controller: 'foos' # works the same as above
post :bar, to: 'foos#bar'



You can also use scope if you want to route multiple routes to the same controller more elegantly:


scope


scope controller: 'foos' do
get :bar
get :baz
end






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard