1

私は持っている...

レーキルート:

edit_report_question
GET
/reports/:report_id/questions/:id/edit(.:format)
questions#edit

question_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'

パラメータ:

{"action"=>"edit", "controller"=>"questions", "report_id"=>"1", "id"=>"1"}

では、なぜ「NilClass:Class の undefined method `model_name'」というエラーが表示されるのでしょうか?

注:現在、各レポートには多くの質問がありますが、最終的にはレポートに多くの質問を含め、多くの質問に属させたいと考えています。

4

2 に答える 2

2

コントローラーのどこにも「@question」を定義していません。コントローラーのアクションは次のようになるはずです

def edit
  @report   = Report.find(params[:report_id])
  @question = @report.questions.find(params[:id]) # replacing @questions with @question
end

@questions はインスタンス変数であり、何も含まれていないため、nil クラス エラーが発生しています。さらに、投稿する前に掘り下げてみてください。ありがとう

于 2013-02-20T06:22:04.650 に答える
1

エラーの原因となっている行はこの行です

simple_form_for [@report, @question]

コントローラーには @question 変数がないため、Rails はフォームのパスを推測できません。@questionsあるべきだと思う@question

@questions = @report.questions.find(params[:id])
于 2013-02-20T06:12:24.820 に答える