Link_to path definition

I am developing a Rails v2.3.2 application .

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.erbI would like to have link, click on it, call the method check_teachersin the controllerHow to determine the path for this link?

app / views / schools / edit.html.erb:

link_to 'Check teachers' WHAT_IS_THE_PATH_HERE
+5
source share
2 answers
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 the config/routes.rbfollowing way:

map.check_teachers, '/school/check_teachers/:id' :controller => :school, :action => :check_teachers

and call the url helper generated by the named route as follows:

link_to 'Check teachers', check_teachers_path(:id => @school.id)

and you can use this identifier to search for teachers in the controller

def check_teachers
  @school = School.find params[:id]
  @teachers = @school.teachers
  ...
end
+22

- routes.rb.

map.connect "schools/:id/check_teachers", :controller => "schools", :action => "check_teachers"

link_to :

link_to "Check teachers", check_teachers_path(:id => @school.id)

, :

 def check_teachers
    @school = School.find_by_id(params[:id])
    # Then you can access the teachers with @school.teachers
  end

, . , .

+2

All Articles