Adding a view and then properly routing it in rails - ruby

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

Related

Understanding 'GET' routes in rails

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

Ruby jsonapi-resources links (relative/absolute)

I am incredibly new to Ruby, so I apologise in advance if this question seems very simple or vague.
Where, when using jsonapi-resources is the base path for JSON API links specified? I wish to change from specifying full URLs to root-relative paths to these resources.
I've found the routes.rb which has
Rails.application.routes.draw do
# Route / to the front-end
root to: 'root#index'
namespace :api do
jsonapi_resources :widgets
// ...more jsonapi_resources calls
end
I am guessing you are looking for something like this?
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
jsonapi_resources :cars, only: [:index]
end
end
end

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).

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')

No route matches [GET] "/"...Sometimes

So I'm a bit of a Rails n00b, so I'll apologize if this is really simple. When I access my server from another computer, I get this message:
No route matches [GET] "/"
And if I try to go to my subpages (Well, currently I only have one), I get something along these lines:
Unknown action
The action 'index' could not be found for AwebpageController
But here's the catch: this only happens sometimes. The rest of the time, the standard RoR homepage loads, and going to wwww.mydomain.com/awebpage serves up the page fine.
My Routes.rb looks like this:
Wobsite::Application.routes.draw do
resources :awebpage
end
And awebpage_controller.rb looks like this:
class AwebpageController < ApplicationController
end
And yes, index.html.erb for Awebpage does exist. It's all so simple that I don't understand what's going wrong. Oh, and my webserver is Thin (Not sure if that matters). Thanks in advance for any help!
You might want to add this to the top of your routes file to set the default controller and page for your site (i.e. http://www.mysite.com/):
root :to => "AwebpageController#index"
To remove the default Ruby on Rails webpage you'll also want to delete the index.html file in your /public/ directory.
Also, although not required, in your controller you're missing the function definition for index.
class AwebpageController < ApplicationController
def index
end
end
Normally you'd do application logic and serve up a view in this function; however if you do nothing RoR automatically loads the view associated with the page (index.html.erb).
If after all this you're still having a problem perhaps explicitly add index to the AwebpageController in your routes file; perhaps rails is only mapping www.mysite.com/Awebpage/ to Awebpage/index and not www.mysite.com/Awebpage/index.

Resources