Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accepting nested attributes for parent model

I'm getting the following error when trying to accept a nested attribute for the User model:

Couldn't find User with ID=1 for Sale with ID=

Sale model:

class Sale < ActiveRecord::Base

   belongs_to :user

   accepts_nested_attributes_for :user

end

User model:

class User < ActiveRecord::Base
   has_many :sales
end

View template:

<%= form_for @sale, :html => {:class => "stagedForm bigForm"} do |f| %>
    <% if @sale.errors.any? %>  
        <div id="errorExplanation">  
            <h2><%= pluralize(@sale.errors.count, "error") %> prohibited this user from being saved:</h2>  
            <ul>  
                <% @sale.errors.full_messages.each do |msg| %>  
                    <li><%= msg %></li>  
                <% end %>  
            </ul>  
        </div>  
    <% end %>

    <fieldset>
    <legend>When</legend>
    <div class="field">
        <%= f.label :start_time %>
        <%= f.datetime_select :start_time, :minute_step => 10, :default => Time.now+1.week, :order => [:day, :month, :year] %>
      </div>
      <div class="field">
        <%= f.label :end_time  %>
        <%= f.datetime_select :end_time, :default => Time.now+( 1.week + 2.hours), :minute_step => 10, :order => [:day, :month, :year] %>
      </div>
    </fieldset> 

    <fieldset>
        <legend>Payment</legend>
        <%= f.fields_for :user do |u| %>
            <%= u.hidden_field :stripe_card_token %>
        <% end %>

    </fieldset>

    <div style="clear:both;"></div>
  <div class="actions">
    <%= f.submit "create", :id => "saveForm" %>
  </div>
<% end %>

Sales controller:

def new
    user = User.find(current_user.id)
    @sale = user.sales.build
    logger.debug "user locations #{user.locations}"
    @locations = user.locations
    1.times { @sale.items.build; @sale.build_location; }
  end


def create
    @sale = Sale.new(params[:sale])
    @sale.user_id = current_user.id

    logger.debug "Sale object!!!  #{@sale.inspect}"
    respond_to do |format|
      if @sale.save
        format.html { redirect_to @sale, notice: 'Sale was successfully created.' }
        format.json { render json: @sale, status: :created, location: @sale }
      else
        format.html { render action: "new" }
        format.json { render json: @sale.errors, status: :unprocessable_entity }
      end
    end
  end
like image 405
Elliot Avatar asked Aug 05 '26 16:08

Elliot


1 Answers

Nested attributes allow you to save attributes on associated records through the parent. So you should have accepts_nested_attributes_for :sales in User model.

And nested attributes comes to rescue when you are trying to Update User Model with sales as child records.

You Should use hidden_field_tag to pass user_id from the view (above associated with Sale) or as sale belongs to User, you can use select tag to select the user.

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

like image 124
naren Avatar answered Aug 07 '26 07:08

naren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!