2016-06-18 7 views
0

私は、カテゴリと呼ばれる別の属性の値に基づいて、サブカテゴリと呼ばれる属性に対して一連の入力値を提供するヘルパーを作ろうとしています。私は行くよどこRails - カテゴリ選択に基づいてサブカテゴリを設定する

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 

は誰でも見ることができます:サブカテゴリ:

<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> 

が、私はその後、私が使用したい入力を持つヘルパーを持っている:

は私が持っているフォームを持っています違う?私は、サブカテゴリの入力フィールドの値を決定するためにカテゴリ値を求めます。

def new 
    @project = Project.new 
    @project.ethics.build 

    def show 



end 



    end 

    # GET /projects/1/edit 
    def edit 
    @project.ethics_build unless @project.ethics 
    end 

プロジェクトは、多くの倫理と倫理を持っている:私は、次のアクションを持つプロジェクトのコントローラを持って

undefined method `category' for nil:NilClass 

コントローラー :私はこれをしようとすると

は、私が言うエラーが出ますプロジェクトに属します。

+0

あなたのコントローラ、特に '@ ethic'インスタンスを設定している部分を表示できますか? – oreoluwa

+0

showメソッドで '@project.ethics_build @ project.ethics'に' @ ethics'を割り当てる必要があります。更新された回答を確認してください。これは今すぐ動作するはずです。 –

答えて

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 

この時点で私の仮定は、あなたがbefore_action方法で@project設定されていることです。

+0

提案してくれてありがとうございます - これを試しましたが、未定義のメソッド 'category 'for Nil:NilClass – Mel

+1

あなたのコントローラ、特に' @ ethic'インスタンスを設定している部分を表示できますか? – oreoluwa

+0

それがうまくいかない場合は、恐らくコントローラから何か間違ったことをしています。コントローラコードも共有できますか? –

関連する問題