Can't create a url that links to a method in my controller
How do I create a link using link_to, that links to a method in my controller. The link I want to create is something like this:
/meetings/10/contacts/2/send_invite
The send_invite method is in the Contacts controller. I'm unsure what the next step as far as how to setup the routes file. I've tried nesting resources but no luck so far. Also, what is the link to specify in the link_to? I've tried various combinations such as send_invite_path, meeting_contact_send_invite_path, but everything throws an error.
My routes file looks like this:
resources :meetings do
resou开发者_如何学JAVArces :contacts, :only => [:send_invite], :as => :send_invite
end
But then when I try to call send_invite_path, it doesn't work.
expanding on austin's answer. This nesting in config/routes.rb
resources :meetings do
resources :contacts do
member do
post :send_invite
end
end
end
would give you these routes
send_invite_meeting_contact POST /meetings/:meeting_id/contacts/:id/send_invite(.:format) {:action=>"send_invite", :controller=>"contacts"}
meeting_contacts GET /meetings/:meeting_id/contacts(.:format) {:action=>"index", :controller=>"contacts"}
POST /meetings/:meeting_id/contacts(.:format) {:action=>"create", :controller=>"contacts"}
new_meeting_contact GET /meetings/:meeting_id/contacts/new(.:format) {:action=>"new", :controller=>"contacts"}
edit_meeting_contact GET /meetings/:meeting_id/contacts/:id/edit(.:format) {:action=>"edit", :controller=>"contacts"}
meeting_contact GET /meetings/:meeting_id/contacts/:id(.:format) {:action=>"show", :controller=>"contacts"}
PUT /meetings/:meeting_id/contacts/:id(.:format) {:action=>"update", :controller=>"contacts"}
DELETE /meetings/:meeting_id/contacts/:id(.:format) {:action=>"destroy", :controller=>"contacts"}
meetings GET /meetings(.:format) {:action=>"index", :controller=>"meetings"}
POST /meetings(.:format) {:action=>"create", :controller=>"meetings"}
new_meeting GET /meetings/new(.:format) {:action=>"new", :controller=>"meetings"}
edit_meeting GET /meetings/:id/edit(.:format) {:action=>"edit", :controller=>"meetings"}
meeting GET /meetings/:id(.:format) {:action=>"show", :controller=>"meetings"}
PUT /meetings/:id(.:format) {:action=>"update", :controller=>"meetings"}
DELETE /meetings/:id(.:format) {:action=>"destroy", :controller=>"meetings"}
You will need to define the route in your routes.rb file and have setup your routes to be nested. As far as the name goes you can define that in your routes.rb file as well by using :as => "some_name"
that will allow you to use link_to some_name_path
.
For more info on routes: http://guides.rubyonrails.org/routing.html
Specifically on nested resources: http://guides.rubyonrails.org/routing.html#nested-resources
精彩评论