0
私はRoR上にWebアプリケーションを持っていますが、ユーザーが画像をアップロードすると問題が発生します。product_id
はproduct_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
です。ありがとう!!
です 'product_id' A外部キーまたはユーザーが入力できる文字列/整数 – Vasfed
@Vasfed 'product_id'は外部キーではありません – d3bug3r
あなたはどのようにそれを"作成 "しますか? – Vasfed