Rails - 基于类别选择填充子类别

时间:2016-06-18 08:28:14

标签: ruby-on-rails simple-form

我正在尝试根据另一个名为category的属性的值,为一个名为:subcategory的属性提供一组输入值的帮助器。

我的表格有:

TableViewController

然后我有一个帮助器,它有我想要用于的输入:subcategory:

RightBarButtonItem

谁能看到我哪里出错了?我希望类别值确定子类别的输入字段的值。

当我尝试这个时,我收到的错误是:

<div class="nested-fields">
<div class="container-fluid">

  <div class="form-inputs">

    <%= f.input :irrelevant, :as => :boolean, :label =>  "Is an ethics review required or applicable to this project?"  %>

    <%= f.input :category, collection: [ "Risk of harm", "Informed consent", "Anonymity and Confidentiality", "Deceptive practices", "Right to withdraw"], :label => "Principle",  prompt: 'select' %>

    <%= f.input :subcategory,  collection: text_for_subcategory(@ethic.category), :label => "Subcategory", prompt: 'select'  %>


    <%= f.input :considerations, as: :text, :label => "Identify the ethics considerations?",  :input_html => {:rows => 8} %>

    <%= f.input :proposal, as: :text, :label => "How will these considerations be managed?",  :input_html => {:rows => 8} %>

    </div>
  </div>
  </div>

控制器 我有一个项目控制器,其行为如下:

module EthicsHelper
    def text_for_subcategory(category)
      if @ethic.category == 'Risk of harm'
            [ "Physical Harm", "Psychological distress or discomfort", "Social disadvantage", "Harm to participants", "Financial status", "Privacy"]
        elsif @ethic.category == 'Informed consent'
            ["Explanation of research", "Explanation of participant's role in research"]
        elsif @ethic.category == 'Anonymity and Confidentiality'
            ["Remove identifiers", "Use proxies", "Disclosure for limited purposes"]
        elsif @ethic.category == 'Deceptive practices'  
            "Feasibility"   
        else @ethic.category == 'Right to withdraw' 
            "Right to withdraw from participation in the project"   
       end
    end  

end

项目有许多道德和道德属于项目。

1 个答案:

答案 0 :(得分:1)

这是你做错了! 在text_for_subcategory(category)方法中,您已将category传递到其中,但您正在if语句中检查@ethic.category。在下面重写它应该有效。

module EthicsHelper
  def text_for_subcategory(category)
    if category == 'Risk of harm'
        [ "Physical Harm", "Psychological distress or discomfort", "Social disadvantage", "Harm to participants", "Financial status", "Privacy"]
    elsif category == 'Informed consent'
        ["Explanation of research", "Explanation of participant's role in research"]
    elsif category == 'Anonymity and Confidentiality'
        ["Remove identifiers", "Use proxies", "Disclosure for limited purposes"]
    elsif category == 'Deceptive practices'  
        "Feasibility"   
    else category == 'Right to withdraw' 
        "Right to withdraw from participation in the project"   
    end
  end
end

请注意,当您在表单中调用它时,您已将@ethic.category传递给方法,因此帮助程序中的category仅用作占位符。

因此,从控制器开始,@ethics根本没有设置。在edit方法中,您需要设置@ethics,因此它显示如下代码。

def show
  @ethics = @project.ethics_build unless @project.ethics
end

此时我的假设是您已在@project方法中设置了before_action