У меня есть веб-приложение на RoR, но проблема заключается в том, что пользователь загружает изображение, product_id
не связан с product_attachments
. Пока я не перейду к следующей форме.Rails, обновите продукт сразу после создания
После был мой контроллер 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
Как я могу обмануть метод create
, поэтому он будет создавать product_id
, когда я создаю product_attachment
? В настоящее время мне нужно продолжить следующий шаг, и он вызывает метод update
для вставки product_id
в product_attachments
table. Благодаря!!
Является 'product_id' внешний ключ или только некоторая строка/целое число, которое пользователь может ввести? – Vasfed
@ Vasfed 'product_id' не является внешним ключом – d3bug3r
Тогда как вы его« создаете »? – Vasfed