I need to post through a link tag, however there is a problem with the link helper. Here is my link tag
// doctors.html.erb
<%= link_to "Ekle", [:add_doctor, @patient], method: :post %>
And my routes.rb
// routes.rb
get 'patients/:id/doctors' => 'patients#get_doctors'
post 'patients/:id/doctors' => 'patients#add_doctor'
I get the error
undefined method `add_doctor_patient_path' for #<#:0x007fd39283a4b0>
How should I use link helper to get rid of the problem?
This line in your routes.rb
shows that you have a get_doctors
route that accepts one argument, :id
:
get 'patients/:id/doctors' => 'patients#get_doctors'
That means you should use the corresponding path helper, get_doctors_path
, and pass it the Patient object:
<%= link_to "Ekle", get_doctors_path(@patient), method: :post %>
PS Your routes are puzzling, since you could accomplish the same thing by using resourceful routes, as recommended by the Rails Guides , instead of defining your own custom get
and post
routes, and in so doing save yourself a lot of trouble:
resources :patients do
resources :doctors
end
This would create eg patient_doctors_path
and new_patient_doctor_path
path helpers that would automatically route to your DoctorsController#get
and DoctorsController#new
methods, respectively, and it would enable you to use eg form_for [ @patient, :doctor ]
. PatientsController is very much the wrong place to put logic for retrieving and modifying Doctors.
Just add an as: option on the routes.
// routes.rb
get 'patients/:id/doctors' => 'patients#get_doctors',as: :patient_get_doctor
post 'patients/:id/doctors' => 'patients#add_doctor', as: :patient_add_doctor
Then in the view:
// doctors.html.erb
<%= link_to "Ekle", patient_add_doctor_path(id: @patient.id), method: :post %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.