繁体   English   中英

提交表单时未保存与ActiveRecord相关的对象-Ruby on Rails

[英]ActiveRecord associated objects not being saved when submitting form - Ruby on Rails

我在提交新表单(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

通话组件 只是分数的标题和说明

belongs_to :score

用户

has_many :qas
has_many :scores, through: :qas

无论出于什么原因,都不会创建分数,但是会创建质量检查。

形成

<%= 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 %>

这是质量检查新方法

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

这是质量检查创建方法

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

关于如何解决此问题的任何想法都很棒。 谢谢你的时间。

尝试这个..

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

因为您需要指定允许嵌套集的哪些属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM