2016-04-19 8 views
0

私はRoR上にWebアプリケーションを持っていますが、ユーザーが画像をアップロードすると問題が発生します。product_idproduct_attachmentsに関連付けられていません。私が次のフォームに進むまでは。続きRailsは、作成した直後に製品を更新します

は私のコントローラ ProductAttachmentsControllerた:私はproduct_attachmentを作成するとき、それはproduct_idが作成されますので、

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 

は、どのように私は、create方法をだますことができますか?現在、私は次のステップを進める必要があり、テーブルにproduct_idを挿入する方法はupdateです。ありがとう!!

+0

です 'product_id' A外部キーまたはユーザーが入力できる文字列/整数 – Vasfed

+0

@Vasfed 'product_id'は外部キーではありません – d3bug3r

+0

あなたはどのようにそれを"作成 "しますか? – Vasfed

答えて

2

あなたはPRODUCT_IDをあなたが(ProductsControllerで)製品を作成すると、セッション変数

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 

に製品添付ファイル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 
関連する問題