ruby - Rails update before save the model -
following section of controller methods:
class productattachmentscontroller < applicationcontroller def create @product_attachment = productattachment.new(product_attachment_params) session[:product_id] --> return product_id respond_to |format| if @product_attachment.save format.html { redirect_to @product_attachment, notice: 'product attachment created.' } # format.json { render :show, status: :created, location: @product_attachment } format.json {render :json => @product_attachment} else format.html { render :new } format.json { render json: @product_attachment.errors, status: :unprocessable_entity } end end end end
model relationships:
class product < activerecord::base has_many :product_attachments, dependent: :destroy end class productattachment < activerecord::base mount_uploader :attachment, attachmentuploader belongs_to :product end
once user upload picture, product_attachments
insert new row in table, how can include product_id
along attachment?
thanks!!
edit
view form:
<section class="photos-section <%= 'hide' unless @show_section == 'photo' %>"> <%= form_for(@product) |f| %> <div class="row"> <div class="col-xs-12 col-sm-12 col-lg-12"> <hr> <div class="form-group"> <div class="fileupload btn btn-large btn-upload"> <span><i class="fa fa-upload" aria-hidden="true" id="file_upload"></i> add photos</span> <input name="file_attachment" type="file" id="file_upload" class="upload"> </div> </div> <hr> </div> </div> <div class="row attachments-container"> <div class="row"> <div class="col-4 row-space-2 h5 invisible" id="js-first-photo-text"> first photo appears in search results! </div> </div> <% if !@product.new_record? %> <% @product.product_attachments.each |attachment| %> <div class="col-xs-6 col-md-4 form-group grayscale"> <img src="<%= attachment.attachment.small.url %>" class="upload-photo-image"> <input type="hidden" name="product_attachment[id][]" value="<%= attachment.id %>"> <button class="delete-photo-btn overlay-btn js-delete-photo-btn delete-attachment" data-toggle="tooltip" data-placement="top" title="delete" data-photo-id="143275119"> <i class="fa fa-trash-o"></i> </button> <button class="cover-photo-btn cover-overlay-btn js-delete-photo-btn cover-attachment" data-toggle="tooltip" data-placement="top" title="set cover photo" data-photo-id="143275119"> <i class="fa fa-picture-o"></i> </button> </div> <% end %> <% end %> <div class="col-xs-6 col-md-4 form-group"> <div class="thumbnail panel photo-item empty-photo-frame" name="empty-photo-frame"> <img src="<%= asset_url('add-image-placeholder.png') %>"> </div> </div> </div> <hr> <div class="row"> <div class="col-xs-12 next-bottom paddingzero"> <div class="col-xs-6"> <% if @product.new_record? %> <a href="#" class="btn-back">back</a> <% else %> <a href="/products/<%= @product.id %>/edit_location" class="btn-back">back</a> <% end %> </div> <div class="col-xs-6"> <% if @product.new_record? %> <a href="" class="btn btn-next pull-right">see next</a> <a href="/products/<%= @product.id %>/edit_price" class="btn btn-next pull-right">see next</a> <% else %> <input type="hidden" name="step-param" value="photo"> <%= f.submit 'see next', :class => 'btn btn-next pull-right photo-submit' %> <% end %> </div> </div> </div> <% end %> </section>
you can check product_attachment_params
think not have product_id
field in parameter, because not see product_id
field in form. can using
@product.product_attachments.build(product_attachment_params)
Comments
Post a Comment