2

使用rails 4和ruby 2.Rails嵌套窗体属性不保存

难以从嵌套窗体保存嵌套属性。我可以看到参数在提交后通过表单传递。我有2个表格:文件和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 %> 

回答

1

尝试增加一个f。前fields_for

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

耶!那工作。谢谢! –

+0

不幸的是,我现在正在收到follwing错误:无效的单表继承类型:销售不是文档 –

+1

的子类:type是用于引用其他模型的名称的保留属性。尝试将其命名为:kind。 – Scymex

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 

您必须通过 “ID” 在document_attributes ......在这个时候没有很好的记录...

干杯

+0

我添加此添加:id到documents_attributes(在我的问题中编辑),但这似乎没有帮助。如果有帮助,也会添加有问题的日志。 –

0

好更改列名的作品,但通常解决这个正确的方法是通过禁用该模型的inheritance_column功能。使用Rails你also.If要使用的名称必须使用保留关键字特权:类型然后就在你的model.rb文件

self.inheritance_column = nil 

希望它可以帮助