Rails嵌套表单属性不保存

时间:2013-07-17 21:59:27

标签: ruby-on-rails nested-forms nested-attributes ruby-on-rails-4

使用rails 4和ruby 2.

从嵌套表单中保存嵌套属性时遇到问题。我可以看到参数在提交后通过表单。我有2个表:documents和salary_reports。我检查了日志,接收插入的唯一表是salary_reports,而不是文档。可能是什么问题?

模型:

class SalaryReport < ActiveRecord::Base

  STATUS = %w[new waiting_for_approval approved dissaproved]

  belongs_to :user

  has_many :documents

  accepts_nested_attributes_for :documents
end


class Document < ActiveRecord::Base

    TYPES = %w[sales valuation mileage expense]

    belongs_to :salary_report
end

控制器

def new
    @salary_report = SalaryReport.new
    @salary_report.documents.build
  end

  def create
    @salary_report = SalaryReport.new(salary_report_params)

    if @salary_report.save
       redirect_to salary_reports_path, notice: "Lönerapporten sparades korrekt!"
    else
      render :new, notice: "Något gick fel när lönerapporten skulle sparas!"      
    end

  end

private
  def salary_report_params
    params.require(:salary_report).permit(:user_id, :approved_by_admin_id, :status,        :comment_from_created_by, :comment_from_admin, :total_sales_since_year_start, :date_sent,       :date_of_approval, :total_buffer, :salary_period, documents_attributes: [:id, :description, :type,      :total])
 end

提交表格后记录:

Started POST "/salary_reports" for 127.0.0.1 at 2013-07-18 10:36:53 +0200
Processing by SalaryReportController#create as HTML
Parameters: {"utf8"=>"✓",     "authenticity_token"=>"kIge4NYVVxnwuhB7JBQdIXXCV+2eNFXy7m1GBrxGbY0=", "salary_report"=>    {"user_id"=>"1", "approved_by_admin_id"=>"", "status"=>"new",     "total_sales_since_year_start"=>"", "date_sent"=>"2013-07-18", "total_buffer"=>"",     "salary_period(3i)"=>"1", "salary_period(2i)"=>"7", "salary_period(1i)"=>"2013",     "comment_from_created_by"=>"comments"}, "documents"=>{"description"=>"something",     "type"=>"sales", "total"=>"2323"}, "commit"=>"Skicka till admin för godkännande"}
   (0.1ms)  begin transaction
   SQL (0.8ms)  INSERT INTO "salary_reports" ("comment_from_created_by", "created_at",     "date_sent", "salary_period", "status", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?,     ?)  [["comment_from_created_by", "comments"], ["created_at", Thu, 18 Jul 2013 08:36:53 UTC     +00:00], ["date_sent", Thu, 18 Jul 2013 00:00:00 UTC +00:00], ["salary_period", Mon, 01 Jul     2013 00:00:00 UTC +00:00], ["status", "new"], ["updated_at", Thu, 18 Jul 2013 08:36:53 UTC     +00:00], ["user_id", 1]]
   (4.4ms)  commit transaction
   (0.2ms)  begin transaction
   (0.2ms)  commit transaction
Redirected to http://localhost:3000/salary_reports
Completed 302 Found in 62ms (ActiveRecord: 5.7ms)

视图如下所示:

<%= form_for @salary_report, :html => { :class => "well"} do |f| %> 
   <%= f.label :salary_period %>
   <%= f.date_select :salary_period, :as => :date, :order => [:month, :year] %>
   <%= f.label :comment_from_created_by %>
   <%= f.text_area :comment_from_created_by %>
   <%= f.label :comment_from_admin %>
   <%= f.text_area :comment_from_admin, :disabled => true %>
   <%= fields_for :documents do |builder| %>
       <%= render "document_fields", :f => builder %>
   <% end %> 
   <%= link_to_add_fields "ny rad", f, :documents %>        
   <%= f.submit "Skicka till admin för godkännande", disable_with: 'Skickar....', :class => "btn btn-success" %>

<% end %>

3 个答案:

答案 0 :(得分:1)

尝试

 private
      def salary_report_params
        params.require(:salary_report).permit(:user_id, :approved_by_admin_id, :status,        :comment_from_created_by, :comment_from_admin, :total_sales_since_year_start, :date_sent,       :date_of_approval, :total_buffer, :salary_period, documents_attributes: [:id, :description, :type,      :total])
     end

你必须在document_attributes中传递“id”......目前还没有很好地记录......

干杯

答案 1 :(得分:1)

尝试添加f。在fields_for

之前
<%= f.fields_for :documents do |builder| %>
  <%= render "document_fields", :f => builder %>
<% end %>

答案 2 :(得分:0)

更改列名可以正常工作,但通常解决此问题的正确方法是禁用该模型的inheritance_column功能。使用Rails你也有权使用保留的关键字。如果你想使用名称:输入,那么只需在 model.rb 文件中添加

self.inheritance_column = nil

希望有所帮助