###前提・実現したいこと
例えばSurvey - Question - Answerという3つのモデル
Survey has_many :questions, :dependent => :destroy, inverse_of: :survey accepts_nested_attributes_for :questions, :reject_if => :all_blank, :allow_destroy => true
Question belongs_to :survey, optional: true,inverse_of: :questions has_many :answers, :dependent => :destroy, inverse_of: :question accepts_nested_attributes_for :answers, :reject_if => :all_blank, :allow_destroy => true
Answer belongs_to :question, optional: true,inverse_of: :answers
となっており、
Questionのindexページから、SurveyのTitleを下記のような形で取得をしたいです。どのようにすればよいでしょうか。
<tbody> <% @questions.each do |question| %> <tr> <td><%= question.survey.title %></td> <td><%= question.qtitle %></td> <td><%= question.body %></td> <td><%= link_to 'Show', question %></td> <td><%= link_to 'Edit', edit_question_path(question) %></td> <td><%= link_to 'Destroy', question, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </tbody>
のようにして、取得したいのですが、no method errorとなってしまいます。
<%= question.survey.inspect %>
とすると実際には、この画像のように値は入っています。
https://i.gyazo.com/08495a657c663b607cf8bdef16ac1732.png
###発生している問題・エラーメッセージ
undefined method `title' for nil:NilClass
###補足情報(言語/FW/ツール等のバージョンなど)
ruby on rails5.0
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/16 09:32
2017/08/16 09:35