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!!
Ist 'product_id' ein Fremdschlüssel oder nur eine Zeichenfolge/Ganzzahl, die der Benutzer eingeben kann? – Vasfed
@Vasfed 'product_id' ist kein Fremdschlüssel – d3bug3r
Dann, wie Sie es" erstellen "? – Vasfed