Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails,在创建产品后立即更新产品_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails Rails,在创建产品后立即更新产品

Ruby on rails Rails,在创建产品后立即更新产品,ruby-on-rails,ruby,Ruby On Rails,Ruby,我在RoR上有web应用程序,但问题是一旦用户上传图像,product\u id与product\u附件不关联。直到我进入下一个表格 下面是我的控制器 产品附件控制器: class ProductAttachmentsController < ApplicationController before_action :set_product_attachment, only: [:show, :edit, :update, :destroy] def index @p

我在RoR上有web应用程序,但问题是一旦用户上传图像,
product\u id
product\u附件不关联。直到我进入下一个表格

下面是我的控制器 产品附件控制器:

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
class ProductAttachmentsController@product_attachment}
其他的
format.html{render:new}
format.json{render json:@product_attachment.errors,status::unprocessable_entity}
结束
结束
结束
def更新
回应待办事项|格式|
if@product_attachment.update(产品_attachment_参数)
format.html{将_重定向到@product_attachment.product,注意:“产品附件已成功更新”。}
format.json{render:show,status::ok,location:@product_attachment}
其他的
format.html{render:edit}
format.json{render json:@product_attachment.errors,status::unprocessable_entity}
结束
结束
结束
def销毁
@产品(附件)销毁
回应待办事项|格式|
format.html{重定向到产品附件url,注意:“产品附件已成功销毁”。}
format.json{head:no_content}
结束
结束
私有的
#使用回调在操作之间共享公共设置或约束。
def set_产品_附件
@product_attachment=ProductAttachment.find(参数[:id])
结束
#永远不要相信来自恐怖网络的参数,只允许白名单通过。
def产品附件参数
参数要求(:产品附件)。许可(:产品id,:附件)
结束
结束

如何欺骗
create
方法,使其在创建
product\u附件时创建
product\u id
?目前我需要继续下一步,它触发
update
方法,在
product\u附件
表中插入
product\u id
。谢谢

您可以将产品附件id存储在会话变量中

  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
创建产品时(在ProductsController中),调用附件并更新产品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

您可以将产品附件id存储在会话变量中

  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
创建产品时(在ProductsController中),调用附件并更新产品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

product\u id
是外键还是用户可以输入的字符串/整数?@Vasfed
product\u id
不是外键那么如何“创建”它呢?我认为如果你给出产品和产品附件之间的关联(有很多,属于)会更好。这将使创建附件变得更容易,因为它将自动设置添加到该产品的附件的产品id。
product\u id
是外键还是用户可以输入的字符串/整数?@Vasfed
product\u id
不是外键那么如何“创建”它?我认为如果你给出产品和产品附件之间的关联(有很多,属于)会更好。这将更容易创建附件,因为它将自动设置添加到该产品的附件的产品id。