Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

link_to path definition

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
like image 301
Mellon Avatar asked Dec 12 '11 16:12

Mellon


1 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 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
like image 151
rubyprince Avatar answered Oct 05 '22 20:10

rubyprince