提交表单时不保存ActiveRecord关联对象- Ruby on Rails



我有问题,我的孩子对象(分数)不被保存时,提交一个新的(Qa)表单。

模型如下:

QA

  belongs_to :user
  has_many :scores, :dependent => :destory
  has_many :call_components, through: :scores
  accepts_nested_attributes_for :scores
得分

 belongs_to :qa
 has_one :call_component

Call Component只是一个乐谱的标题和描述

belongs_to :score
用户

has_many :qas
has_many :scores, through: :qas

无论出于什么原因,当提交帖子时没有创建分数,但是QA是。

<%= form_for [@qa], role: "form" do |f| %>
  <%= f.label :call_id %>
  <%= f.number_field :call_id, :autofocus => true, class: "form-control monospaced-control", placeholder: "Call Id", required: "" %>
  ... more fields
<% CallComponent.all.each do |comp| %> <!-- Usually is an array of about 5 components, so there 5 scores -->
      <h4><b><%= comp.title.to_s.capitalize %></b></h4>
      <p><%= comp.description.to_s.capitalize %></p>
       <%= f.fields_for :scores, @qa.scores  do |builder|  %>
           <%= builder.label :score, "Score" %>
           <%= builder.number_field :score, :autofocus => true, class: "form-control monospaced-control", placeholder: "Score", required: ""%>
           <%= builder.label :comments, "Comments" %><br />
           <%= builder.text_area :comments, :autofocus => true, class: "form-control monospaced-control", placeholder: "Score", required: ""%>
           <%= builder.hidden_field :call_component_id, :value => comp.id %>
          <% end %>
<% end %>

这是QA New方法

def new
   @qa = Qa.new
   @qa.scores.build
   # Tried it this way too
   #@score = Score.new
   #@score.build_qa # then in the view linking the form like @score.qa, this didnt work.
end
这里是QA创建方法
def create
    @qa = Qa.new(qa_params)
    @qa.final_score = @qa.scores.sum(:score).to_i
    @qa.user_id = current_user.id
    if @qa.save
      redirect_to qas_path, notice: "New qa published!"
    else
      flash[:alert] = "Qa not published!"
      render :new
    end
  end
  def qa_params
     params.require(:qa).permit(:call_id,...,:scores_attributes)
  end

任何关于如何解决这个问题的想法将是很棒的。感谢您的宝贵时间。

try this.

def qa_params
     params.require(:qa).permit(:agent_user_id, :call_id, :call_date, :case_number, :completion_date, scores_attributes: [:score,:comments,:call_component_id] )
  end