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 

希望它可以幫助