Rails - 为类别和子类别填充选择菜单

时间:2016-06-19 22:58:45

标签: ruby-on-rails associations simple-form helper

我试图在我的Rails 4应用中制作一个有两个选择标签的表单。一个用于类别,另一个用于子类别。我希望子类别中的选项可以通过为类别选择来确定。

目前我有这种形式:

<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(@category), :label => "Subcategory", prompt: 'select'  %>



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

我还有一个帮手:

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

当我尝试这个时,无论我在类别字段中做出什么选择,子类别字段都只填充了最后一个属性(撤销权限)。

我上面所做的事情有些不对,我无法弄清楚它是什么。

谁能看到我出错的地方?

1 个答案:

答案 0 :(得分:0)

将类添加到选择字段:

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

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

添加以下javascript代码。

$(document).ready(function() {
  $(".main_category").change(function(){
    var category = $(".main_category").val(),
        sub_category = $(".sub_category"),
        options = [],
        str = "";
    sub_category.find('option').remove();

    if(category == 'Risk of harm'){
      options = ["Physical Harm", "Psychological distress or discomfort", "Social disadvantage", "Harm to participants", "Financial status", "Privacy"]
    }
    else if(category == 'Informed consent'){
      options = ["Explanation of research", "Explanation of participant's role in research"]   
    }
    else if(category == 'Anonymity and Confidentiality'){
      options = ["Remove identifiers", "Use proxies", "Disclosure for limited purposes"]
    }
    else if(category == 'Deceptive practices'){
      options = ["Feasibility"]
    }
    else if(category == 'Right to withdraw'){
      options = ["Right to withdraw from participation in the project"]  
    }
    if(options.length > 0){
      for(i=0;i<options.length;i++){
        str = '<option value="' + options[i] + '">' + options[i] + '</option>'
        sub_category.append(str);
      }
      sub_category.val(options[0]);
    }
  });
});

我还没有测试过代码。