Two quite similar routing settings really is confusing.
resources :authors do
resources :books
end
and
resources :authors do
member do
resources :books
end
end
As we all know, rails will generate the following routings :
writer_book GET /writers/:writer_id/books/:id(.:format) books#show
and
book GET /writers/:id/books/:id(.:format) books#show
How is this member option useful?
One can just not using member option and set params[:writer_id] in books_controller and be done with it right?
Does this will have a bad affect when the application gets bigger? What are the consequences?
The member and collection methods are meant to add additional RESTful actions to resources
resources :writers do
member do
post :favorite
end
collection do
get :unpublished
end
end
They are not intended for nesting resources
# bad
resources :writers do
member do
resources :books
end
end
# good
resources :writers do
resources :books
end
What are the consequences?
Using member here will result in the route
GET /writers/:id/books/:id(.:format)
Which means that the id param is ambigous! It could be either the id of the book or the author! Not good! Not using member would give us params[:writer_id] which we can use to fetch the parent record.
GET /writers/:writer_id/books/:id(.:format) books#show
See:
Rails Routing from the Outside In
Related
This is a polymorphic association!
resources :professors, :labs do
member do
resources :teaching_assistants
end
end
I want to have index for professors teaching assistants, labs teaching assistants and teaching assistants like so:
/professors/id/teaching_assistants
/labs/id/teaching_assistants
/teaching_assistants
All these routes are pointing to
teaching_assistants#index. Do i have to explicitly point the nested routes to an action in in the professors and labs controller? How would I do that/alter the routes file?
resources :professors, :labs do
member do
resources :teaching_assistants, except: :index
end
end
get '/professors/id/teaching_assistants' => professors#assistantsindex
get '/labs/id/teaching_assistants' => labs#assistantsindex
Using resources is for creating all the basic RESTful pointers (index/create/read/update/delete). It sounds like your teaching assistants are a 'one off' listing page, in which case you would simply use get to create the route.
Try this:
resources :professors, :labs do
member do
# /professors/id/teaching_assistants > professors#teaching_assistants
# /labs/id/teaching_assistants > labs#teaching_assistants
get 'teaching_assistants'
end
end
# /teaching_assistants
get 'teaching_assistants'
I defined the following custom Rails route in routes.rb:
resources :projects do
get 'members'
end
This results in the following route (output from rake routes):
project_members GET /projects/:project_id/members(.:format)
What I would like, though, is for the route to map to this instead (change :project_id to :id)
project_members GET /projects/:id/members(.:format)
How can I make that happen?
Is members a nested resource? If so define it as one, and understand that this is why you have :project_id in the route, because :id in a nested resource is used by the final child item - you can't have multiple nested resources all using the same variable to define their id.
resources :projects do
resources :members
end
Add in a third level of nesting and it becomes a bit clearer to explain:
resources :projects do
resources :members do
resources :colours
end
end
With this nesting you could visit app/projects/:project_id/members/:member_id/colours/:id which would be served by the colours controller, which knows that :id defines an instance of that controllers model, and any other named id's belong to other resources.
Otherwise I think you just need to define it as a member method:
resources :projects do
member do
get 'members'
end
end
This tells the route that the action members is a non-resource action belonging to an instance of project, which I think should sort you out, but be sure it's the right thing to do.
See section 2.10 of Rails Routing from the Outside In
I need some help with routes for nested controllers. I can't figure out from the Rails guide docs by myself.
I have the following controllers in a rails 3.2 app:
/app/controllers/organizations_controller.rb (class OrganizationsController)
/app/controllers/organization/events_controller.rb (class Organization::EventsController)
then, in routes.rb
resources :organizations, path: 'org' do
resources :events
member do
get 'confirm'
end
end
end
running rake routes shows (only the relevant part for my issue):
organization_event GET /org/:organization_id/events/:id(.:format) events#show
The URL is ok, the route name is also ok, but the mapping to the "controller/action" is not right. Not as I want it to be. It should be organization/events#show.
What am I missing? How can I point this route to the correct controller. I chose to put the events_controller in the organization folder, because I already have another events_controller placed in the root of the controllers folder, and they have different purposes.
Thank you
namespace :organization do
resources :events
member do
get "confirm"
end
end
end
More info here.
EDIT
Sorry, didn't understand you correctly.
resources :organizations, path: 'org' do
resources :events, :module => "organization"
member do
get 'confirm'
end
end
end
Does that fit your needs?
I have a nested resource that looks like this:
resources :events
resources :attendances
post 'update_email'
end
end
and it shows me routes that look like this (left out most of the standard REST routes for brevity):
event_attendance GET /events/:event_id/attendances/:id
event_attendance_update_email POST /events/:event_id/attendances/:event_attendance_id/update_email
So, why is it that when I add new routes, they have a different id parameter?
Ack, figured this out almost immediately after posting.
The problem is that I didn't specify that it was a route for a member resource:
resources :events
resources :attendances
member do
post 'update_email'
end
end
end
produces what I wanted:
event_attendance GET /events/:event_id/attendances/:id
event_attendance_update_email POST /events/:event_id/attendances/:id/update_email
i have a problem as following.
i have a resources setup in my routes.rb file as following
resources :users do
resources :sub_transactions
end
resources :sub_transactions do
collection do
get :income
get :expenditure
end
end
Now what is the correct route that i should write so that i can generate the following routes
users/1/sub_transactions/income
users/1/sub_transactions/expenditure
where income and expenditure are not ids
Have you tried adding those calls to the nested sub_transactions resource? Like:
resources :users do
resources :sub_transactions do
collection do
get :income
get :expenditure
end
end
end
I'm not in a position to test at the moment but that would be the logical starting point (I figure).