Ruby on rails Rails中的多个嵌套表单和简单表单
我是一个Rails新手,创建了一个约会网站OkCupid的克隆作为练习。我正在制作表单,以显示一个问题、其可能的答案和可能的SoughtAnswers(用户选择的答案表示他/她希望其他用户如何回答相同的问题) 示例数据:Ruby on rails Rails中的多个嵌套表单和简单表单,ruby-on-rails,forms,simple-form,Ruby On Rails,Forms,Simple Form,我是一个Rails新手,创建了一个约会网站OkCupid的克隆作为练习。我正在制作表单,以显示一个问题、其可能的答案和可能的SoughtAnswers(用户选择的答案表示他/她希望其他用户如何回答相同的问题) 示例数据: <%= simple_form_for @answered_question do |aq_form| %> <%= aq_form.input :question_id, as: :hidden %> <%= @answered_ques
<%= simple_form_for @answered_question do |aq_form| %>
<%= aq_form.input :question_id, as: :hidden %>
<%= @answered_question.question.body %>
<%= aq_form.simple_fields_for @answered_question.question.answers do |answer_fields| %>
<%= answer_fields.input :body %>
<% end %>
<div class='sought-answer-header'>
People I'm most interested in would answer:
</div>
<%= aq_form.simple_fields_for @answered_question.sought_answers do |sa_fields| %>
<%= sa_fields.input :answer, label_method: :body, value_method: :id, include_blank: :false, label_html: { class: 'sought-answer-body' } %>
<% end %>
<%= aq_form.button :submit %>
<% end %>
问题:“你有多少个孩子?”
回答:“2”(其他可能的回答包括“0”、“1”和“3+”)
SoughtAnswers:“0”、“1”、“2”
问题:
<%= simple_form_for @answered_question do |aq_form| %>
<%= aq_form.input :question_id, as: :hidden %>
<%= @answered_question.question.body %>
<%= aq_form.simple_fields_for @answered_question.question.answers do |answer_fields| %>
<%= answer_fields.input :body %>
<% end %>
<div class='sought-answer-header'>
People I'm most interested in would answer:
</div>
<%= aq_form.simple_fields_for @answered_question.sought_answers do |sa_fields| %>
<%= sa_fields.input :answer, label_method: :body, value_method: :id, include_blank: :false, label_html: { class: 'sought-answer-body' } %>
<% end %>
<%= aq_form.button :submit %>
<% end %>
我使用simple_form gem创建表单来收集用户对问题的回答。我不知道如何正确显示表单并创建一个新的AnsweredQuestion,它存储用户、问题、答案和SoughtAnswers,以获得对问题的完整回答
我应该如何改变我的模型和形式来正确地创建一个被回答的问题
以下是相关文件的摘录部分:
问题.rb
class Question < ActiveRecord::Base
validates :body, presence: true, uniqueness: true
has_many :answers, inverse_of: :question, dependent: :destroy
has_many :answered_questions, inverse_of: :question, dependent: :destroy
has_many :sought_answers, through: :answers, dependent: :destroy
accepts_nested_attributes_for :answers, reject_if: :all_blank
class Answer < ActiveRecord::Base
belongs_to :question, inverse_of: :answers
has_many :answered_questions, inverse_of: :answer, dependent: :destroy
has_many :sought_answers, dependent: :destroy
class SoughtAnswer < ActiveRecord::Base
has_one :user, through: :answered_question
belongs_to :answer
belongs_to :answered_question, inverse_of: :sought_answers
class User < ActiveRecord::Base
has_many :answered_questions, dependent: :destroy
has_many :questions, through: :answered_questions
has_many :sought_answers, dependent: :destroy
class AnsweredQuestion < ActiveRecord::Base
belongs_to :user
belongs_to :question
belongs_to :answer
has_many :sought_answers, inverse_of: :answered_question, dependent: :destroy
accepts_nested_attributes_for :sought_answers, reject_if: :all_blank
def show
@answered_question = AnsweredQuestion.new
@answered_question.user = current_user
@answered_question.question = current_user.random_unanswered_question
@answered_question.question.answers.each do |answer|
@answered_question.sought_answers.build(answer: answer)
end
end
回答问题/\u form.html.erb:
<%= simple_form_for @answered_question do |aq_form| %>
<%= aq_form.input :question_id, as: :hidden %>
<%= @answered_question.question.body %>
<%= aq_form.simple_fields_for @answered_question.question.answers do |answer_fields| %>
<%= answer_fields.input :body %>
<% end %>
<div class='sought-answer-header'>
People I'm most interested in would answer:
</div>
<%= aq_form.simple_fields_for @answered_question.sought_answers do |sa_fields| %>
<%= sa_fields.input :answer, label_method: :body, value_method: :id, include_blank: :false, label_html: { class: 'sought-answer-body' } %>
<% end %>
<%= aq_form.button :submit %>
<% end %>
表单当前返回此错误:
NoMethodError in Dashboard#show
undefined method `has_attribute?' for #<Answer::ActiveRecord_Associations_CollectionProxy:0x007fd7ee8124f0>
Extracted source (around line #21):
<%= answer_fields.input :body %>
仪表板中的命名错误显示
未定义的方法'has_attribute'#
提取的源(第21行附近):
非常感谢 你好,Mark,你能在AnsweredQuestionsController中发布“new”方法的内容吗?嘿@lavilet,我添加了DashboardController#show方法,该方法在加载表单之前调用。谢谢你能提供的任何帮助!