ruby on rails - Creating new records with has_many through -
i trying create simple form create valuations (offers) specific goods( spare arts). valuation may have many parts selected using select form. using classes listed below. have created join table valuation_lines store valuations. when using form there no errors records missing... unfortunatelly got stuck , have no idea how create form save records in valuation_lines table?
i appreciate on issue! new rails got stuck 2 days on , have no idea how overcome this...
class valuation < activerecord::base has_many :valuation_lines has_many :parts, :through => :valuation_lines accepts_nested_attributes_for :parts end class valuationline < activerecord::base belongs_to :part belongs_to :valuation end class part < activerecord::base has_many :valuation_lines has_many :valuations, :through => :valuation_lines end
further form creating new valuations looks like:
<%= form_for(@valuation) |f| %> <% if @valuation.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@valuation.errors.count, "error") %> prohibited valuation being saved:</h2> <ul> <% @valuation.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :description %><br> <%= f.text_area :description %> </div> <div class="field"> <%= f.fields_for :part |builder| %> <%= builder.label :part %><br> <%= builder.collection_select :part_id, part.all, :id, :name %> <% end %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
join table:
create_table "valuation_lines", force: true |t| t.integer "valuation_id" t.integer "part_id" t.integer "pruchase_price" end
valuation controller:
# /valuations/new def new @valuation = valuation.new end # post /valuations # post /valuations.json def create @valuation = valuation.new(valuation_params) respond_to |format| if @valuation.save format.html { redirect_to @valuation, notice: 'valuation created.' } format.json { render action: 'show', status: :created, location: @valuation } else format.html { render action: 'new' } format.json { render json: @valuation.errors, status: :unprocessable_entity } end end end
Comments
Post a Comment