我有......
佣金路线:
edit_report_question
GET
/reports/:report_id/questions/:id/edit(.:format)
questions#edit
questions_controller.rb:
def edit
@report = Report.find(params[:report_id])
@questions = @report.questions.find(params[:id])
end
report.rb:
has_many :questions
question.rb:
belongs_to :report
edit.html.haml:
.textbox
= render "shared/notice"
%h1 Edit Question
= render "form"
= render "actions"
_form.html.haml:
= simple_form_for [@report, @question] do |f|
= f.error_notification
= f.association :report
= f.input :description
= f.input :blueprint_name, :label => "Blueprint Name"
= f.input :blueprint_url, :label => "Blueprint URL"
= f.association :element, :label => "Website Element"
= f.association :standard
- if params[:action]=~ /edit/
= f.association :fields, :as => :select
= f.button :submit, :id => 'submit_question'
PARAMS:
{"action"=>"edit", "controller"=>"questions", "report_id"=>"1", "id"=>"1"}
为什么我得到一个“未定义的方法`model_name'用于NilClass:Class”错误?
注意:目前每个报告都有很多问题,但最终我希望报告包含很多问题并且属于很多问题。
答案 0 :(得分:2)
你没有在控制器的任何地方定义'@question',我认为你的控制器动作应该像
def edit
@report = Report.find(params[:report_id])
@question = @report.questions.find(params[:id]) # replacing @questions with @question
end
您收到的是nil类错误,因为@questions是一个实例变量,并且没有任何内容。另外请在发布之前尝试挖掘。感谢
答案 1 :(得分:1)
导致错误的行是此行
simple_form_for [@report, @question]
因为在控制器中,你没有@question变量,Rails无法推断出表单的路径。我认为@questions
应该有@question
@questions = @report.questions.find(params[:id])