首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails -基于类别选择的子类别填充

Rails -基于类别选择的子类别填充
EN

Stack Overflow用户
提问于 2016-06-18 08:28:14
回答 1查看 145关注 0票数 0

我试图为一个名为:子类别的属性提供一组输入值,它是基于另一个名为: based的属性的值的。

我有一份表格,上面有:

代码语言:javascript
复制
<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>

然后我有一个助手,它有我想要用于:子类别的输入:

代码语言:javascript
复制
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

有人能看到我哪里出了问题吗?我希望类别值确定子类别的输入字段的值。

当我尝试这样做时,我会发现一个错误:

代码语言:javascript
复制
undefined method `category' for nil:NilClass

controller I有一个具有以下操作的项目控制器:

代码语言:javascript
复制
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

项目有许多伦理道德,伦理属于工程。

EN

回答 1

Stack Overflow用户

发布于 2016-06-18 08:36:43

这就是你做错的事!在text_for_subcategory(category)方法中,将category传递给它,但在if语句中检查@ethic.category。在下面重写它应该是可行的。

代码语言:javascript
复制
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传递到方法中,因此助手中的category只是充当占位符。

因此,在控制器中,根本没有设置@ethics。在edit方法中,需要设置@ethics,使其显示如下代码。

代码语言:javascript
复制
def show
  @ethics = @project.ethics_build unless @project.ethics
end

现在我的假设是,您已经在一个@project方法中设置了before_action

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37894906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档