2016-06-20 2 views
0

にヘルパーテキストを使用する私はプロジェクトと倫理と呼ばれるモデルを持って関連する足場Railsの4 - どのように関連する部分

にビューからヘルパーメソッドにアクセスする方法を把握しようとしています。団体は以下のとおりです。私の倫理ヘルパーで

Project has_many :ethics 
Ethic belongs_to :project 

、私が持っている:

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 

フォルダを表示するその後、私のプロジェクトでは、私が持っている部分と呼ばれる_ethics.html.erbあります

<% @project.ethics.each do | project_ethics_issue | %> 
         <strong>ETHICS CONSIDERATION: <%= project_ethics_issue.text_for_subcategory(@category) %></strong> 
         <%= project_ethics_issue.considerations %> 

        <% end %> 
を私のプロジェクトのコントローラで

、私が試してみました:

include EthicsHelper 
私が間違って行ってきたところ

undefined method `text_for_subcategory' for #<Ethic:0x007fedc57b7b58> 

は誰でも見ることができます:私はこれをしようとすると3210

は、私が言うエラーが出ますか?ファイル_ethics.html.erb

答えて

0

は、あなただけのように、このコードを変更:

<% @project.ethics.each do | project_ethics_issue | %> 
    <strong>ETHICS CONSIDERATION: <%= text_for_subcategory(@category).join(",") %></strong> 
    <%= project_ethics_issue.considerations %> 
<% end %> 

アップデート:私はあなたのファイルethics_helper.rbで見た

が、あなたのロジックコードが正しくないようです。

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"] 
    elsif category == 'Right to withdraw'  
     ["Right to withdraw from participation in the project"] 
    else 
     [] 
    end 
    end 
end 
+0

ありがとう@Khanh - ヘルパーのコンテンツを表示するように機能しましたが、[""]の中にテキストが表示されます。フォームを表示する際にエラーが発生するため、これらのタグをヘルパーから削除することはできません。 – Mel

+0

'array'にテキストを置くので、'研究の説明、研究における参加者の役割の説明 'としてテキストを表示したい場合は、 'text_for_subcategory(@category).join("、 ")' –

+0

配列には1つのものしかありません。配列の内容はフォームに正しく表示されますが、show partialでは[""]が表示されます。配列の選択された要素の内容を表示したいだけです(この場合は配列内の唯一のものです)。[""]を表示したくありません。 – Mel

関連する問題