Rails nested form attributes not saving -
using rails 4 , ruby 2.
having trouble saving nested attributes nested form. can see parameters coming through form after submitting. have 2 tables: documents , salary_reports. have checked logs , table receives insert salary_reports, not documents. problem?
models:
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
controller
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
log after submitting form:
started post "/salary_reports" 127.0.0.1 @ 2013-07-18 10:36:53 +0200 processing salaryreportcontroller#create 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 "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 http://localhost:3000/salary_reports completed 302 found in 62ms (activerecord: 5.7ms)
the view looks this:
<%= form_for @salary_report, :html => { :class => "well"} |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 |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 %>
try adding f. before fields_for
<%= f.fields_for :documents |builder| %> <%= render "document_fields", :f => builder %> <% end %>
Comments
Post a Comment