2016-06-19 11 views
0

2つの選択タグを持つRails 4アプリケーションでフォームを作成しようとしています。 1つはカテゴリ用で、もう1つはサブカテゴリ用です。サブカテゴリのオプションは、カテゴリの選択によって決定されます。私もとヘルパー持っRails - カテゴリとサブカテゴリのセレクトメニューの設定

<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 

を、サブカテゴリフィールドには、最後の属性のみ(取り消す権利)が設定されます。

私が上記で行ったことに何か問題があり、それが何であるか把握できません。

誰かが間違っているのを誰でも見ることができますか?

+0

この機能を追加するにはJavaScriptを使用する必要があります。 – webster

+0

@rahul - あなたはそれについてどうやって行くべきかについてのガイダンスはありますか? – Mel

+0

下記の私の答えを見つける。 – webster

答えて

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]); 
    } 
    }); 
}); 

私はコードをテストしていません。

+0

私にこれを設定する時間を取ってくれてありがとう。私はそれを試しても、私はもともと遭遇したのと同じエラーを取得します。私もjQueryに変更しようとしました。おそらく 'else if'を 'elsif'に変更する必要がありますか? – Mel

+0

ブラウザコンソールにエラーはありますか? – webster

+0

いいえ - コンソールにエラーが表示されない – Mel

関連する問題