Rails 4 - 如何在相关部分中使用帮助文本

Rails 4 - How to use helper text in an associated partial

我正在尝试找出如何从关联脚手架中的视图访问辅助方法

我有一个名为 Project 和 Ethic 的模型。这些协会是:

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>

谁能看出我哪里出错了?

在文件_ethics.html.erb中,您只需将这段代码修改为:

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