使用simple_form在Rails中使用多个嵌套表单

时间:2015-05-13 01:58:59

标签: ruby-on-rails forms simple-form

我是一个Rails新手,创建一个约会网站OkCupid的克隆作为练习。我正在表单上显示一个问题,它可能的答案,以及它可能的SoughtAnswers(这是用户选择的答案,表明他/她希望其他用户如何回答同一个问题)。

示例数据:

问题:"你有多少个孩子?"

答案:" 2" (其他可能的答案包括" 0"," 1"和" 3 +")

SoughtAnswers:" 0"," 1"," 2"

问题:

我正在使用simple_form gem创建表单以收集用户对问题的回复。我迷失了如何正确显示表单并创建一个新的AnsweredQuestion,它存储了User,Question,Answer和SoughtAnswers,以便对一个问题做出一个完整的回答。

如何更改模型和表单以正确创建AnsweredQuestion?

以下是相关文件的摘录部分:

question.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

answer.rb

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

sought_answer.rb

class SoughtAnswer < ActiveRecord::Base
  has_one :user, through: :answered_question
  belongs_to :answer
  belongs_to :answered_question, inverse_of: :sought_answers

user.rb

class User < ActiveRecord::Base
  has_many :answered_questions, dependent: :destroy
  has_many :questions, through: :answered_questions
  has_many :sought_answers, dependent: :destroy

answered_question.rb

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

AnsweredQuestionsController

  def create
    @answered_question = AnsweredQuestion.new(answered_question_params)
    @answered_question.user = current_user
    @answered_question.sought_answers.each do |sa|
      sa.answered_question = @answered_question
    end

  private
    def answered_question_params
      params.require(:answered_question).permit(:question_id, :answer_id, sought_answers_attributes: [:answer_id])
    end

answered_questions / _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 %>

dashboard_controller.rb

  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

表单当前返回此错误:

NoMethodError in Dashboard#show

undefined method `has_attribute?' for #<Answer::ActiveRecord_Associations_CollectionProxy:0x007fd7ee8124f0>
Extracted source (around line #21):

    <%= answer_fields.input :body %>

非常感谢!

0 个答案:

没有答案