我试图为一个名为:子类别的属性提供一组输入值,它是基于另一个名为: based的属性的值的。
我有一份表格,上面有:
<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>然后我有一个助手,它有我想要用于:子类别的输入:
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有人能看到我哪里出了问题吗?我希望类别值确定子类别的输入字段的值。
当我尝试这样做时,我会发现一个错误:
undefined method `category' for nil:NilClasscontroller I有一个具有以下操作的项目控制器:
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项目有许多伦理道德,伦理属于工程。
发布于 2016-06-18 08:36:43
这就是你做错的事!在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传递到方法中,因此助手中的category只是充当占位符。
因此,在控制器中,根本没有设置@ethics。在edit方法中,需要设置@ethics,使其显示如下代码。
def show
@ethics = @project.ethics_build unless @project.ethics
end现在我的假设是,您已经在一个@project方法中设置了before_action。
https://stackoverflow.com/questions/37894906
复制相似问题