将参数传递给符号调用的方法

时间:2014-07-22 14:51:25

标签: ruby ruby-on-rails-4 simple-form-for

我正在尝试调用自定义方法,让我们说'get_divisions_by_school_id'并将变量传递给simple_form_for中的该方法

而不是:divisions,我想在以下代码中调用:get_divisions_by_school_id(@ school.id):

<%= f.grouped_collection_select(:division_id, @teacher.standards, :divisions, :id, :id, :name,  {:class => "input-box-width1", :required => true},{:multiple=>true}) %>

我想要类似的东西:

<%= f.grouped_collection_select(:division_id, @teacher.standards, :get_divisions_by_school_id(@school.id), :id, :id, :name,  {:class => "input-box-width1", :required => true},{:multiple=>true}) %>

1 个答案:

答案 0 :(得分:0)

您可以创建一个获取学校ID的类方法并执行以下操作:

class Teacher < ActiveRecord::Base 
    def self.get_divisions_by_school_id(school_id)
        # Foobarbaz.
    end
end

并使用它:

<%= f.grouped_collection_select(:division_id, @teacher.standards, Teacher.get_divisions_by_school_id(@school.id), :id, :id, :name,  {:class => "input-box-width1", :required => true},{:multiple=>true}) %>