Initialize associated object in rails -


i have trouble fill associated objects.

i have associated object, try fill local db's tables form data, can fill building , object composed : light_reseller, stage, furniture, location. don't find how fill component "points" of stage , furniture !

here model building.rb

class building < activerecord::base  has_many :stage, dependent: :destroy has_many :furniture, dependent: :destroy has_one :light_reseller, dependent: :destroy has_one :location, dependent: :destroy accepts_nested_attributes_for :light_reseller, :location, :stage, :furniture, :allow_destroy => true end  class lightreseller < activerecord::base   belongs_to :building end  class location < activerecord::base   belongs_to :building end  class stage < activerecord::base   belongs_to :building   has_many :points, dependent: :destroy   accepts_nested_attributes_for :points end  class furniture < activerecord::base   belongs_to :building   has_many :points, dependent: :destroy   accepts_nested_attributes_for :points end  class point < activerecord::base   belongs_to :furniture   belongs_to :stage end 

i use form fill objects :

<%= form_for (@building) |f| %> <% if @building.errors.any? %>     <div id="error_explanation">       <h2><%= pluralize(@building.errors.count, "error") %> prohibited store being         saved:</h2>        <ul>         <% @building.errors.full_messages.each |message| %>             <li><%= message %></li>         <% end %>       </ul>     </div> <% end %>  <div class="field">   <h2>store</h2>   <%= f.label :name %><br>   <%= f.text_field :name, class: "form-control" %>   <%= f.fields_for :location |loc| %>       <%= loc.label :address %><br>       <%= loc.text_field :address, class: "form-control" %>       <%= loc.label :city %><br>       <%= loc.text_field :city, class: "form-control" %>   <% end %>   <%= f.fields_for :light_reseller |lr| %>       <%= lr.label :"light_reseller name" %><br>       <%= lr.text_field :name, class: "form-control" %>   <% end %>   <h2>stage</h2>   <%= f.fields_for :stage |ft| %>       <%= ft.label :name %><br>       <%= ft.text_field :name, class: "form-control" %>       <%= ft.fields_for :point |pt| %>           <%= pt.label :"point value" %><br>           <%= pt.text_field :val, class: "form-control" %>       <% end %>       <h3>stage entries</h3>       <%= ft.fields_for :entrie |et| %>           <%= et.label :"entrie name" %><br>           <%= et.text_field :name, class: "form-control" %>           <%= et.fields_for :point |ept| %>               <%= ept.label :"point value" %><br>               <%= ept.text_field :val, class: "form-control" %>           <% end %>       <% end %>   <% end %>    <%= f.fields_for :furniture |ft| %>       <h2>furniture</h2>       <%= ft.label :name %><br>       <%= ft.text_field :name, class: "form-control" %>       <%= ft.fields_for :point |pt| %>           <%= pt.label :"point value" %><br>           <%= pt.text_field :val, class: "form-control" %>       <% end %>       <%= ft.fields_for :point |pt| %>           <%= pt.label :"point value" %><br>           <%= pt.text_field :val, class: "form-control" %>       <% end %>   <% end %> </div> <div class="actions">   <%= f.submit "add store", class: "btn btn-default"%> </div> <% end %> 

and here controller :

class buildingscontroller < applicationcontroller   before_action :set_building, only: [:show, :edit, :update, :destroy]    def index     @buildings = building.all   end   def new    @building = building.new    @building.build_location    @building.build_light_reseller    @building.furniture.new    @building.stage.build  end   def show  end   def create    @building = building.new(buildings_params)    respond_to |format|     if @building.save       format.html { redirect_to @building, notice: 'building created.' }       format.json { render :index, status: :created, location: @building }     else       format.html { render :new }       format.json { render json: @building.errors, status: :unprocessable_entity }     end    end  end   def update   respond_to |format|    if @building.update(buildings_params)     format.html { redirect_to @building, notice: 'building updated.' }     format.json { render :show, status: :ok, location: @buildings }   else     format.html { render :edit }     format.json { render json: @buildings.errors, status: :unprocessable_entity }     end    end   end    def destroy   @building.destroy    respond_to |format|     format.html { redirect_to buildings_path, notice: 'building destroyed.' }     format.json { head :no_content }    end   end    private    def set_building      @building = building.find(params[:id])    end     def buildings_params      params.require(:building).permit(:name, location_attributes:[:address, :city], light_reseller_attributes:[:name], furniture_attributes:[:name, point_attributes:[:val]], stage_attributes:[:name, point_attributes:[:val]])    end 

so when submit form, controller receive these data:

"building"=>{"name"=>"fnac", "location_attributes"=>{"address"=>"12 bird road", "city"=>"paris"}, "light_reseller_attributes"=>{"name"=>"marc"}, "stage_attributes"=>{"0"=>{"name"=>"first", "point"=>{"val"=>"1235"}}}, "furniture_attributes"=>{"0"=>{"name"=>"shelf", "point"=>{"val"=>"45"}}}} 

you have relation has_many :points furniture , stage, need change

<%= ft.fields_for :point |pt| %> 

to

<%= ft.fields_for :points |pt| %> 

and change point_attributes points_attributes in buildings_params.

try below changes along above.

class building < activerecord::base    has_many :stages, dependent: :destroy   has_many :furnitures, dependent: :destroy   has_one :light_reseller, dependent: :destroy   has_one :location, dependent: :destroy   accepts_nested_attributes_for :light_reseller, :location, :stages, :furnitures, :allow_destroy => true end   def new    @building = building.new    @building.build_location    @building.build_light_reseller    @building.furnitures.build    @building.stages.build  end  def buildings_params    params.require(:building).permit(:name, location_attributes:[:address, :city], light_reseller_attributes:[:name], furnitures_attributes:[:name, points_attributes:[:val]], stages_attributes:[:name, points_attributes:[:val]]) end 

<%= f.fields_for :stage |ft| %> <%= f.fields_for :stages |ft| %>

and

<%= f.fields_for :furniture |ft| %> <%= f.fields_for :furnitures |ft| %>


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -