2016-04-19 11 views
0

Ich habe die Web-App auf RoR, aber das Problem ist, sobald der Benutzer das Bild hochladen, die product_id ist nicht mit der product_attachments verbunden. Nicht bevor ich mit dem nächsten Formular fortfahre.Schienen, aktualisieren Sie das Produkt direkt nach dem Erstellen eines

Nach waren mein Controller ProductAttachmentsController:

class ProductAttachmentsController < ApplicationController 
    before_action :set_product_attachment, only: [:show, :edit, :update, :destroy] 


    def index 
    @product_attachments = ProductAttachment.all 
    end 

    def show 
    end 

    def new 
    @product_attachment = ProductAttachment.new 
    end 

    def create 
    @product_attachment = ProductAttachment.new(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 :json => @product_attachment} 
     else 
     format.html { render :new } 
     format.json { render json: @product_attachment.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def update 
    respond_to do |format| 
     if @product_attachment.update(product_attachment_params) 
     format.html { redirect_to @product_attachment.product, notice: 'Product attachment was successfully updated.' } 
     format.json { render :show, status: :ok, location: @product_attachment } 
     else 
     format.html { render :edit } 
     format.json { render json: @product_attachment.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @product_attachment.destroy 
    respond_to do |format| 
     format.html { redirect_to product_attachments_url, notice: 'Product attachment was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_product_attachment 
     @product_attachment = ProductAttachment.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def product_attachment_params 
     params.require(:product_attachment).permit(:product_id, :attachment) 
    end 
end 

Wie kann ich die create Methode verleiten, so wird es eine product_id erstellen, wenn ich product_attachment erstellen? Zur Zeit muss ich nächsten Schritt fortfahren und es update Methode auslösen, um product_id in product_attachments Tabelle einzufügen. Vielen Dank!!

+0

Ist 'product_id' ein Fremdschlüssel oder nur eine Zeichenfolge/Ganzzahl, die der Benutzer eingeben kann? – Vasfed

+0

@Vasfed 'product_id' ist kein Fremdschlüssel – d3bug3r

+0

Dann, wie Sie es" erstellen "? – Vasfed

Antwort

2

Sie speichern das Produkt Befestigung ID in einer Sitzungsvariablen

if @product_attachment.save 
    session[:product_attachment] = @product_attachment.id ### HERE! 
    format.html { redirect_to @product_attachment, notice: 'Product attachment was successfully created.' } 
    format.json {render :json => @product_attachment} 
    else 
    format.html { render :new } 
    format.json { render json: @product_attachment.errors, status: :unprocessable_entity } 
    end 

Wenn Sie das Produkt (in der Products) erstellen erinnern die Anlage und aktualisieren Sie die product_id

if @product.save 
    if session[:product_attachment] 
    ProductAttachment.find(session[:product_attachment]).update_attribute(:product_id, @product.id) 
    session[:product_attachment] = nil 
    end 
    ... 
end 
Verwandte Themen