2016-04-20 11 views
-1

folgende Speicher waren mein Abschnitt Controller-Methoden:Rails-Update vor, das Modell

class ProductAttachmentsController < ApplicationController 

def create 
    @product_attachment = ProductAttachment.new(product_attachment_params) 
    session[:product_id] --> return product_id 
    respond_to do |format| 
     if @product_attachment.save 

     format.html { redirect_to @product_attachment, notice: 'Product attachment was successfully 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 

Modell Beziehungen:

class Product < ActiveRecord::Base 
has_many :product_attachments, dependent: :destroy 

end 

class ProductAttachment < ActiveRecord::Base 

    mount_uploader :attachment, AttachmentUploader 
    belongs_to :product 
end 

Sobald das Bild Benutzer hochladen, die product_attachments Einsatz neuen Zeilen in der Tabelle, jetzt, wie kann ich product_id zusammen mit dem Anhang einschließen?

enter image description here

Dank !!

EDIT

Ansicht Form:

<section class="photos-section <%= 'hide' unless @show_section == 'photo' %>"> 
    <%= form_for(@product) do |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"> 
    Your first photo appears in search results! 
    </div> 
</div> 
      <% if [email protected]_record? %> 
       <% @product.product_attachments.each do |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 as 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> 
+0

können Sie das Formular für die Befestigung Schöpfung enthalten? Sie können dies tun, wenn Sie ** fields_for ** verwenden und ** access_nested_attributes_for ** in Ihrem Produktmodell hinzufügen. Sie können auch einen Blick auf http://railscasts.com/episodes/196-nested-model-form-part-1?view=asciicast –

+0

@ JeffreyMCastro werfen, überprüfen Sie das Ansichtsformular in Bearbeitung. – d3bug3r

+0

Für den Teil add_photos können Sie in der Tat die Felder f.fields_for und accepts_nested_attributes_for im Produktmodell verwenden und vergessen, die zulässigen Parameter im Produkt-Controller zu aktualisieren. Schau dir den Railscast Link an den ich dir sehr gut geholfen habe :) –

Antwort

1

Sie können product_attachment_params überprüfen Ich denke, wir haben nicht product_id Feld in diesem Parameter, weil ich nicht product_id Feld in Ihrem Formular sehen. Sie können auch

@product.product_attachments.build(product_attachment_params) 
0

Sie die in Ihrem Controller folgende Verwendung:

class ProductAttachmentsController < ApplicationController 

def create 
    product_id = session[:product_id] 
    # Look for the product by product_id and create your attachment from there. 
    @product_attachment = Product.find(product_id).build(product_attachment_params) 

    respond_to do |format| 
     if @product_attachment.save 
     format.html { redirect_to @product_attachment, notice: 'Product attachment was successfully 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 
Verwandte Themen