У меня есть много ко многим отношениям между DailyQuestionSets и Вопросы. У меня есть отношения один ко многим между Вопросами и Ответами. У меня возникают проблемы при разработке: как создать правильную вложенную форму, которая могла бы получить набор Вопросов с меткой, показывающей текст вопроса, а также поле формы с ответом, которое было бы опубликовано в модели Ответа.
Модель:
class Question < ApplicationRecord
has_many :dailyquestions, foreign_key: 'dailyquestion_id'
has_many :dailyquestionsets, :through => :dailyquestions
end
class DailyQuestion < ApplicationRecord
belongs_to :daily_question_set
belongs_to :question
end
class DailyQuestionSet < ApplicationRecord
has_many :daily_questions, foreign_key: 'question_id'
has_many :questions, :through => :daily_questions, :source => :question
end
class Answer < ApplicationRecord
belongs_to :users
belongs_to :questions
belongs_to :dailyquestionset
end
Вид:
<p id="notice"><%= notice %></p>
<%= form_tag('/answers/answerquestions') do %>
<% @questions.each do |q| %>
<%= fields_for '@answers[]', Answer do |a| %>
<div class="field">
<%= q.label :content %><br>
<%= a.text_field :answer_text %>
</div>
<% end %>
<% end %>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
Контроллер:
def answerquestions
@dailyquestionset = DailyQuestionSet.get_today_dailyquestionset
@questions = @dailyquestionset.questions
@answers = []
@questions.count.times do
@answers << Answer.new
end
Проблема в том, что я не могу получить значение метки из q (Вопрос) в представлении, и это не удивительно, поскольку я сделал дикое предположение о том, как создать этот синтаксис вложенной формы.
undefined method `label' for #<Question:0x00007f54383ce748>
Extracted source (around line #8):
<div class="field">
<%= q.label :content %><br>
<%= a.text_field :answer_text %>
</div>
Любые идеи о том, как правильно обращаться с формой, наиболее ценятся:)