How to add alias in routes.rb - ruby-on-rails

How to add alias to route file?
Something like this:
/rules => 'posts/1', param: id => 1
Is it possible define it in routes.rb
I want
/rules => posts/1
not
/rules/1 => posts/1

Well, let's look at the official Rails routing guide:
You can also define other defaults in a route by supplying a hash for the :defaults option. This even applies to parameters that you do not specify as dynamic segments.
So then:
get "/rules" => "posts#show", :defaults => { :id => "1" }

try this
get '/rules', to: redirect('/posts/1')
http://guides.rubyonrails.org/routing.html#redirection

Related

rails showing . instead of / in url

I have a static_controller with index action where an id is needed
My routes
get 'faqs' => 'static#main'
get 'faqs/:id' => 'static#index'
but if I use this path faqs_path(faq_id) I get
http://localhost:5000/faq.1 instead of http://localhost:5000/faq/1
Could someone tell me how to fix this. Thank you..
You may need to name them properly:
get 'faqs' => 'static#main', as: 'faqs'
get 'faqs/:id' => 'static#index', as: 'faq'
Normally you do this with resources where you try to adhere to REST conventions, but in your case if you really need them this way you'll have to coach the router.
Check with rake routes that the names are correct. You may have been calling faqs_path with the id going in as the optional :format specifier.
I think your problem here is same routes name, you can change your name as:
get 'faqs/:id' => 'static#index'
get 'faqs' => 'static#main'
to
get 'faq/:id' => 'static#index'
get 'faqs' => 'static#main'
And your path is: faq_path(faq_id)
Tell me if it didn't work.
Can you try. faqs_path(:id => faq_id)

Rails custom routes with params

I am trying to display params in my url so i have added
patient_record_path(:limit => 10)
I am now trying to correctly route this.
Currently i am getting the error
No route matches {:action=>"show", :controller=>"patient_record", :limit=>10}
I am currently using the route
match 'patient_record/show&limit', :to => 'patient_record#show'
You should not add the limit to your route. Just simple define your route like this:
match 'patient_record/show', :to => 'patient_record#show', :as => 'patient_record_show'
A better solution however would be
resources :patient_records
This would create the following path helpers:
patient_records_path => "/patient_records" => 'patient_record#index'
new_patient_record_path => "/patient_records/new" => 'patient_record#new'
edit_patient_record_path(:id) => /patient_records/:id/edit => 'patient_record#edit'
patient_record_path(:id) => "/patient_records/:id" => 'patient_record#show'
Update: wrong use of path helper
I have looked at your question again and found another bug: the path helper for show needs a record. The correct use is:
# path to show
patient_record_path(#patient_record, :limit => 10)
# path to index
patient_records_path(:limit => 10)

How can I set rails routes to redirect some urls to another controller with parameter and default action?

I want to mix the two line below with one.
I guess it is possible but I don't know.
any ideas you have?
match '/alias4' => redirect("/original/4") # redirect to original#show => 4
match '/alias4(/:action)' => redirect("/original/4/%{action}") # redirect to some actions (not restful)
match '/alias4(/:action)' => redirect("/original/4/%{action}"), :defaults => { :action => "show" }
should work.
Source: http://guides.rubyonrails.org/routing.html#defining-defaults .

Rails hide controller name

i have match ":id" => "people#show" in my routes.rb
now i can access http://localhost:3000/1
but,in views <%= link_to 'Show', people %> it will generate http://localhost:3000/people/1 ,
i want to it to be http://localhost:3000/1
You could do something like this to ensure that only numeric ids are matched:
match '/:id' => 'people#show', :constraints => {:id => /\d+/}
A good alternative might be to use some kind of identifier, even if it's not the controller name: http://localhost:3000/p/1. This will at least ensure that if you add other controllers and actions you don't end up having to change your routing structure.
You could write a custom route to match that in config/routes.rb. At the bottom of your routes.rb file you will have a route like match ':controller(/:action(/:id(.:format)))'
or something like resources :people. You might have to write a route that matches the route type you want.
You have to create a named route.
match ':id' => 'people#show', :as => :person
And fix your views to use your new method person_path(user_id).

problem in routes

i want to change the default route in RoR to what i want:
consider the following example...
:controller/:action/:id
which will give you the route in the browser as:
http://localhost:3000/controller/action/id
now i want to change it to...
http://localhost:3000/this-is-what-i-want/id
we can get an alias for the controller and the action as well like...
resources :controller, :as => "my-custom-name"
and if you want to have the alias for the action, then
resources :controller, :path_names => { :action => 'my-custome-name-1', :action => 'my-custome-name-2' }
BUT i want to change the controller and the action at once... if u noticed the above http://localhost:3000/this-is-what-i-want/id path in the question...
need help...
thanks in advance...
You need a named route.
In Rails2:
map.a_name 'this-is-what-i-want/:id', :controller => 'controller_name', :action => 'action_name'
In Rails3:
match 'this-is-what-i-want/:id' => 'controller_name#action_name'
You want to be using Rest routes, rather than controller/action
I'm going to use "balls" instead of "this-is-what-i-want"
resources :balls
Then, when you link to a ball, do link_to(ball.name, ball).
This will give you a link of http://localhost:3000/balls/45
This rails rest cheatsheet is a good start.

Resources