I am developoing a Rails v2.3.2 app.
I have a controller:
class SchoolController < ApplicationController
...
def edit
@school=School.find_by_id params[:id]
end
def check_teachers
@teachers = @school.teachers
...
end
end
in app/views/schools/edit.html.erb
I would like to have a link
, click on it will trigger the check_teachers
method in the controller, how to define the path for this link
?
app/views/schools/edit.html.erb :
link_to 'Check teachers' WHAT_IS_THE_PATH_HERE
link_to 'Check teachers', :action => :check_teachers, :id => @school.id
or
link_to 'Check teachers', "/school/check_teachers/#{@school.id}"
or you can define a named-route in config/routes.rb
like this:
map.check_teachers, '/school/check_teachers/:id' :controller => :school, :action => :check_teachers
and call the url-helper generated by the named-route like this:
link_to 'Check teachers', check_teachers_path(:id => @school.id)
and you can use this id to find teachers in the controller
def check_teachers
@school = School.find params[:id]
@teachers = @school.teachers
...
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With