私は、カテゴリと呼ばれる別の属性の値に基づいて、サブカテゴリと呼ばれる属性に対して一連の入力値を提供するヘルパーを作ろうとしています。私は行くよどこ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
コントローラー :私はこれをしようとすると
は、私が言うエラーが出ますプロジェクトに属します。
あなたのコントローラ、特に '@ ethic'インスタンスを設定している部分を表示できますか? – oreoluwa
showメソッドで '@project.ethics_build @ project.ethics'に' @ ethics'を割り当てる必要があります。更新された回答を確認してください。これは今すぐ動作するはずです。 –