I'm trying to create two routes in my Rails application with optional parameters.
Here's my routes file:
get '(a)(/:area_id/)l/:location_id/(*url_title)', to: 'locations#show', as: :location
get 'a/:area_id/(*url_title)', to: 'areas#show', as: :area
Navigating to the following URLs correctly routes me to the right controller:
http://localhost:3000/a/1/l/2/seo-friendly-title.html
http://localhost:3000/a/1/seo-friendly-title.html
However, navigating to this url does not work:
http://localhost:3000/l/2/seo-friendly-title.html
I receive a No route matches error. Is it possible to make the a/:area_id portion optional when the l/:location_id portion is present?
Using Rails 4.2.4.
You could just add another route...
get 'l/:location_id/(*url_title)', to: 'locations#show'
There's no reason why two routes can't map to the same action.
Related
Recently I've been working with rails. I have a form with form_tag, which receives a helper post from routes.rb. I have the same route in get to access this form, and I have the route in posrt to be able to send the data of the previous form:
get 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', as: :inscribir_student_monitoring
post 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', as: :inscribir_student_monitoring_post
My question is, how can I separate these routes? Since, according to investigating, this can cause problems, since both have the same address, and really if it is causing me problems, since when entering this link the form is executed automatically.
Do you necessarily have to separate the two routes? And if that is true, how can I do it?
I tried something similar to what rails do with respect to the method get new and post create method but adapted to my case, but it has not worked for me.
Thank you.
Do you necessarily have to separate the two routes?
No! Unless your both routes have a same name, it won't be a problem. For instance if you have routes like below
get 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', as: :inscribir_student_monitoring
post 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', as: :inscribir_student_monitoring
then Rails would rails an exception like so
ArgumentError: Invalid route name, already in use:
'inscribir_student_monitoring' You may have defined two routes with
the same name using the :as option, or you may be overriding a route
already defined by a resource with the same naming. For the latter,
you can restrict the routes created with resources as explained
here:
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
Since your routes have a different name, this won't be a worry. Though you can shorten your code by using match
match 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', via: [:get, :post]
I was wondering how to display an action when specifying a controller url. For example; say I have DemoController.rb, and views/demo/index.html.erb file. Also I've specified the route in routes.rb
get 'demo/index'
How can I setup the project so that when I type "localhost:3000/demo" it renders the same layout as "localhost:3000/demo/index"?
Currently when I type in "localhost:3000/demo" I get a "no route matches" error.
You need a get 'demo' => 'demo#index' instead for that URL to work.
That said, you might want to use Rails conventions and maybe pluralize the controller and use RESTful routes instead:
resources :demos, only: :index
In a rails 3 application following match route is defined in routes.rb
match 'accounts/:account_type/:account_id/edit_account' => 'accounts#edit_account'
In controller i redirect to this URL
redirect_to "/accounts/account/#{account_id.to_s}/edit_account"
It works fine but i need a route helper instead of manually building the URL something like this
edit_account_accounts_path(account_id: id, account_type: 'some_type')
Is there any way to do this ?
you can try
match 'accounts/:account_type/:account_id/edit_account', to: 'accounts#edit_account', as: 'edit_account_accounts'
for more help see The Lowdown on Routes in Rails 3
This question already has answers here:
:as in rails routes.rb
(3 answers)
Closed 8 years ago.
In ruby on rails, what does the "as:" do in route?
Example: http://guides.rubyonrails.org/routing.html 1.2
You can also generate paths and URLs. If the route above is modified to be:
get '/patients/:id', to: 'patients#show', as: 'patient'
and your application contains this code in the controller:
#patient = Patient.find(17)
and this in the corresponding view:
<%= link_to 'Patient Record', patient_path(#patient) %>
In routes as: option is used to make url or path helpers for that particular route. If you look at your route:
get '/patients/:id', to: 'patients#show', as: 'patient'
You have specified as: 'patient' which will enable rails to make patient_path and patient_url helpers
patient_path will give you /patient/:id and patient_url will give you domain/patient/:id
If you run rake routes in your terminal it will list all the routes of your application with there corresponding helper methods. For details checkout path and url helpers
It defines what the route helpers will look like:
patient_path(#patient)
It's the path name which is generated from your routes
For example, if you have the following:
#config/routes.rb
resources :bones, as: :skeleton
You'll get routes as skeleton_path for the bones controller
--
You have to remember that Rails' routing structure is based around "resources" (handled by controllers). Your routes, therefore, should be structured around the controllers your application has
If you want to change the "name" of a particular controller path helper (from bones to skeleton for example), you'll be able to use the as option
I'm working with Rails 3.2 in a project, and I need to create a route for all controllers.
For now, the route is:
resources :people do
collection do
get 'search_for'
end
end
I need thsi "search_for" action for all controllers in application.
Thanks.
you can use this route
match "/:controller/search_for" => redirect("/%{controller}/search_for")
UPDATE: the route above will not work and will cause a redirect loop error simply because it causes a 301 redirect to the same route. solution is to just use
match "/:controller/search_for"
be sure to place this route above all routes so routes that go to the show action will not override this route like.
Simply put this above all routes as:
get ':controller/search_for'