2

使用 rails 4 和 ruby​​ 2。

从嵌套表单中保存嵌套属性时遇到问题。我可以看到参数在提交后通过表单传递。我有 2 个表:文档和薪水报告。我检查了日志,唯一收到插入的表是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 %>
4

3 回答 3

1

尝试添加一个 f。在 fields_for 之前

<%= f.fields_for :documents do |builder| %>
  <%= render "document_fields", :f => builder %>
<% end %>
于 2013-07-18T13:15:39.463 回答
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”...目前没有很好的文档记录...

干杯

于 2013-07-18T07:36:07.097 回答
0

改变列名是可行的,但通常解决这个问题的正确方法是禁用该模型的继承列功能。使用 Rails,您也有权使用保留关键字。如果您想使用名称:type那么只需将其添加到您的model.rb文件中

self.inheritance_column = nil

希望能帮助到你

于 2013-12-26T12:35:41.407 回答