Understanding 'GET' routes in rails - ruby

In my routes file I have specified:
resources :cards do
end
Apart from the basic CRUD routes I have another route which is as follows:
get '/cards/get_schema' => 'cards#get_schema'
When I hit this endpoint, I'm actually taken to cards#show. Why does this happen?

One route generated by resources :cards is get '/cards/:id'. Can you see the issue? get_schema is recognized as id. Try this one
resources :cards do
get 'get_schema', on: :collection
end
Or just put that route on top
get '/cards/get_schema' => 'cards#get_schema'
resources :cards

Rails is treating get_schema as the id of a card. The solution is to reorder the route declarations, like so:
get '/cards/get_schema' => 'cards#get_schema'
resources :cards do
end
This way the get_schema route will be matched before the show route.

It depends on the order of routes defined.
Order 1
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :cards do
end
get '/cards/get_schema' => 'cards#get_schema'
end
Running routes
rake routes
Output
~/D/p/p/s/console_test> rake routes
Prefix Verb URI Pattern Controller#Action
cards GET /cards(.:format) cards#index
POST /cards(.:format) cards#create
new_card GET /cards/new(.:format) cards#new
edit_card GET /cards/:id/edit(.:format) cards#edit
card GET /cards/:id(.:format) cards#show #<========
PATCH /cards/:id(.:format) cards#update
PUT /cards/:id(.:format) cards#update
DELETE /cards/:id(.:format) cards#destroy
cards_get_schema GET /cards/get_schema(.:format) cards#get_schema #<========
Since show expects cards/:id and is above /cards/get_schema it gets routed to cards#show
Order 2
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get '/cards/get_schema' => 'cards#get_schema'
resources :cards do
end
end
Running routes
rake routes
Output
~/D/p/p/s/console_test> rake routes
Prefix Verb URI Pattern Controller#Action
cards_get_schema GET /cards/get_schema(.:format) cards#get_schema #<========
cards GET /cards(.:format) cards#index
POST /cards(.:format) cards#create
new_card GET /cards/new(.:format) cards#new
edit_card GET /cards/:id/edit(.:format) cards#edit
card GET /cards/:id(.:format) cards#show #<========
PATCH /cards/:id(.:format) cards#update
PUT /cards/:id(.:format) cards#update
DELETE /cards/:id(.:format) cards#destroy
In this scenario /cards/get_schema will be top level and won't conflict with cards#show

Related

How to write route for the scenario below

I have a JSON route path that looks like this:
/jobs/{jobid}/employees/{employeeid}/departments/cubes/{cubeid}/register
The following route works fine when there was no cube and cubed:
resources :departments, only: [] do
get 'register', on: :collection
end
I tried doing this:
namespace :departments do
resources :cubes, only: [] do
get 'register', on: :collection
end
end
What change do I need to do in the above code so it will work with the route path?
When I add:
resources :jobs do
resources :employees do
namespace :departments do
get 'cubes/:cube_id/register',
to: 'cubes#register'
end
end
end
to my routes.rb and then run rake routes, I get this line in my output:
GET /jobs/:job_id/employees/:employee_id/departments/cubes/:cube_id/register(.:format) departments/cubes#register
It's not a good practice to nest resources more than 1 level
http://guides.rubyonrails.org/routing.html
Resources should never be nested more than 1 level deep.
http://weblog.jamisbuck.org/2007/2/5/nesting-resources
Rule of thumb: resources should never be nested more than 1 level
deep. A collection may need to be scoped by its parent, but a specific
member can always be accessed directly by an id, and shouldn’t need
scoping (unless the id is not unique, for some reason).

Adding a view and then properly routing it in rails

Ok everyone,
i am using rails 4.2.0
so i have created three scaffolds but needed to add an additional view to one of my models.
I have a Site and Resident model and what i am looking to accomplish is to create a resident thst ties directly back to the site from a link on the site show page.
In my site controller i have the following.
def new_site_resident
#site.residents.create(resident_params)
end
AND
def resident_params
params.require(:resident).permit(:site_id, :unit_number, :f_name, :m_name, :l_name, :dob)
end
im my routes folder i have
get "/sites/:id/new_site_resident"
and have tried get "/sites/new_site_resident
when i use the get "/sites/new_site_resident it comes back with this error:
Couldn't find Site with 'id'=new_site_resident
^^ i am sure this is telling me it is looking for a site with that as the id.
when i use get "/sites/id/new_site_resident" in routes.rb and then try to go to:
http://localhost:3000/sites/1/new_site_resident it gives the error of:
No route matches [GET] "/sites/1/new_site_resident"
not too sure where to go with this? but as i said before i want to be able to look at the site and then be able to directly create a resident in that link that ties back to the site without having to manually enter the site id as the end uses would know that the site id would be.
Current routes.rb file.
Rails.application.routes.draw do
root 'home#index'
resources :sites
resources :vehicles
resources :residents
get "/sites/:id/new_site_resident"
devise_for :users, controllers: { registrations: "registrations" }
To create route to your action, you can use resources:
resources :sites do
member do
get :new_site_resident
end
end
then you can remove
get "/sites/:id/new_site_resident"
Just move
get "/sites/:id/new_site_resident"
line to the start of the routes file. File should look like
Rails.application.routes.draw do
root 'home#index'
get "/sites/:id/new_site_resident"
resources :sites
resources :vehicles
resources :residents
devise_for :users, controllers: { registrations: "registrations" }
Or try member routes to define this route like
resources :sites do
member do
get :new_site_resident
end
end

Rails routing issue - URL works, though button to link to it won't

I'm writing what is currently a very inelegant program to generate fitness plans, and have an issue with the routing. I want buttons on my index page linking to certain bodyparts and a plan generator, and the pages themselves are working when I navigate to them directly. However, the buttons on my index view won't work, kicking out a routing error: 'No route matches [POST] "/exercises/index"'.
For example, dropping the URL for '/exercises/legs' or '/exercises/generator' into my browser loads the page as it should be, though <%= button_to "Legs", 'exercises/legs' %> (as well as redirecting to exercises_legs_path and every other option I've thought of) gives the error.
Sure this is something pretty straightforward I'm missing (very new to this), and any advice would be great!
The database currently contains columns for the :id, ':move' (i.e. press up) and ':bodypart' (i.e. legs).
Here are my routes:
Helper HTTP Verb Path Controller#Action
GET /exercises/:bodypart(.:format) exercises#bodypart
exercises_generator_path GET /exercises/generator(.:format) exercises#generator
exercises_index_path GET /exercises/index(.:format) exercises#index
root_path GET / exercises#index
exercises_path GET /exercises(.:format) exercises#index
POST /exercises(.:format) exercises#create
new_exercise_path GET /exercises/new(.:format) exercises#new
edit_exercise_path GET /exercises/:id/edit(.:format) exercises#edit
exercise_path GET /exercises/:id(.:format) exercises#show
PATCH /exercises/:id(.:format) exercises#update
PUT /exercises/:id(.:format) exercises#update
DELETE /exercises/:id(.:format) exercises#destroy
And my routes.rb file:
Rails.application.routes.draw do
get '/exercises/:bodypart', to: 'exercises#bodypart'
get '/exercises/generator', to: 'exercises#generator'
get 'exercises/index'
root :to => 'exercises#index'
resources :exercises
end
Thanks in advance, and let me know if there's anything else I've got that would help with this.
Rails.application.routes.draw do
get '/exercises/:bodypart', to: 'exercises#bodypart', as: 'exercises_bodypart'
get '/exercises/generator', to: 'exercises#generator'
get 'exercises/index'
root :to => 'exercises#index'
resources :exercises
end
instead of
Rails.application.routes.draw do
get '/exercises/:bodypart', to: 'exercises#bodypart'
get '/exercises/generator', to: 'exercises#generator'
get 'exercises/index'
root :to => 'exercises#index'
resources :exercises
end
Usage:
exercises_bodypart_path('legs')

How can I create a Rails 3 route that will match all requests and direct to one resource / page?

I have a rails app (Rails 3.0) that I need to temporarily take out of service. While this is in effect, I want to create a new route that will direct all requests to a single piece of static content. I have a controller set up to serve my static pages.
I tried something like this:
match '*' => 'content#holding'
and
match '*/*' => 'content#holding'
to match a wildcard route as described here:Rails 3 route globbing without success.
This is probably a really simple answer, but I couldn't figure it out.
/EDIT/
Forgot to mention that I did have this rule at the very top of my routes.rb file.
Rails needs to bind the url parameters to a variable, try this:
match '*foo' => 'content#holding'
If you also want to match /, use parenthesis to specify that foo is optional:
match '(*foo)' => 'content#holding'
I did this just yesterday and first came up with the solution that klochner shows.
What I didn't like about this is the fact that whatever you enter in the URL, stays there after the page loads, and since I wanted a catch all route that redirects to my root_url, that wasn't very appealing.
What I came up with looks like this:
# in routes.rb
get '*ignore_me' => 'site#unknown_url'
# in SiteController
def unknown_url
redirect_to root_url
end
Remember to stick the routes entry at the very bottom of the file!
EDIT:
As Nick pointed out, you can also do the redirect directly in the routes file.
I ran into something like this where I had domain names as a parameter in my route:
match '/:domain_name/', :to => 'sitedetails#index', :domain_name => /.*/, :as =>'sitedetails'
The key piece to this was the /.*/ which was a wildcard for pretty much anything. So maybe you could do something like:
match '/:path/', :to => 'content#holding', :path=> /.*/, :as =>'whatever_you_want'
Where in "routes.rb" is this line located?
To have priority over other routes, it has to be placed first.
As an alternative, you can look into this: http://onehub.com/blog/posts/rails-maintenance-pages-done-right/
Or this: Rails: admin-only maintenance mode

No route matches controller

In my rails 3 app, I have a route which shows up as follows while calling rake routes:
topic_snippets GET /topics/:topic_id/snippets(.:format) {:action=>"index", :controller=>"snippets"}
In routes.rb
resources :topics do
member do
get 'get_topics'
end
resources :snippets, :only => [:index]
end
In my view, I am referencing this route as follows (where #name = "snippets"):
<%= send("topic_#{#name}_path")%>
When executing the previous line, I get the following routing error, not sure why:
No route matches {:controller=>"snippets"}
Update: I found another question whose responses seem to imply that the above should work: Dynamically construct RESTful route using Rails
Thanks
Anand
OK, I found it - Ryan's comment provided the clue.
I wasnt passing in #topic, which is required. If I remove #topic, it tries to just get at /snippets/ which doesn't have a route. I set #topic to a valid topic before calling this line and it works. Thanks, Ryan!
Have you tried
<%= send(eval("topic_#{#name}_path"), #topic)%>

Resources