【问题标题】:ROR Scaffold Destroy redirect_toROR 脚手架销毁 redirect_to
【发布时间】:2015-07-18 18:45:19
【问题描述】:

我实际上构建了一个引用用户(设计)和产品的“报价”脚手架。我可以在特定产品页面上添加报价。但是,我意识到,当我尝试删除某个商品时,默认情况下它会重定向到 products_url。如何将其重定向回特定的产品页面?当我创建评论时,它会重定向到特定的产品页面。删除不这样做。 我试过使用

原始代码

class OffersController < ApplicationController
before_action :set_offer, only: [:show, :edit, :update, :destroy]

def index
 @offers = Offer.all
 end

def show
end

def new
 @offer = Offer.new
 end

 # GET /offers/1/edit
 def edit
 end

 # POST /offers
 # POST /offers.json
 def create
@product = Product.find(params[:product_id])
@offer = @product.offers.new(offer_params)
@offer.user = current_user

respond_to do |format|
  if @offer.save
    format.html { redirect_to @product, notice: 'Offer was successfully created.' }
    format.json { render json: @product, status: :created, location: @offer }
  else
    format.html { render action: "new" }
    format.json { render json: @offer.errors, status: :unprocessable_entity }
  end
  end
 end

 # PATCH/PUT /offers/1
 # PATCH/PUT /offers/1.json
 def update
   respond_to do |format|
     if @offer.update(offer_params)
       format.html { redirect_to @offer, notice: 'Offer was successfully updated.' }
      format.json { render :show, status: :ok, location: @offer }
     else
    format.html { render :edit }
    format.json { render json: @offer.errors, status: :unprocessable_entity }
  end
  end
  end

     # DELETE /offers/1
  # DELETE /offers/1.json
   def destroy
   @offer.destroy
    respond_to do |format|
      format.html { redirect_to product_url, notice: 'Offer was successfully destroyed.' }
      format.json { head :no_content }
    end
   end

  private
    # Use callbacks to share common setup or constraints between actions.
   def set_offer
    @offer = Offer.find(params[:id])
    end

     # Never trust parameters from the scary internet, only allow the white list through.
    def offer_params
      params.require(:offer).permit(:product_id, :priceOffer, :user_id)
   end
 end

我尝试修改

def destroy
@offer.destroy
respond_to do |format|
  format.html { redirect_to @product, notice: 'Offer was successfully destroyed.' }
  format.json { head :no_content }
end
end

它实际上显示了我的错误。 26 实际上是 offer_id。它实际上应该重定向到 http://localhost:3000/products/18 。它向我展示了提取的来源,如下所示。

Couldn't find Product with 'id'=26
def set_product
  @product = Product.find(params[:id])
end

【问题讨论】:

    标签: ruby-on-rails ruby path destroy


    【解决方案1】:

    我不确定我是否理解了这个问题,但我认为您只需要将产品的 id 作为附加参数传递,例如:

    = link_to 'destroy', offer_path(@offer, product_id: @product.id), method: :delete
    

    然后在你的控制器中使用

    redirect_to product_path(params[:product_id])
    

    【讨论】:

      【解决方案2】:

      在销毁方法中执行此操作。

      产品 =@offer.product

      redirect_to :产品

      您使用的@product 未设置。所以我们需要在这里设置product_id。 这就是为什么我们通过关系从 offer 变量中获取产品 ID

      【讨论】:

        【解决方案3】:

        您在 set_product 中所做的只是使用 params[:id] 来查找产品,但 params[:id] 在您调用 destroy 时是指 offer_id,这就是您收到 RecordNotFoundError 的原因。我想你可以写这个。

        def set_product
          # maybe you should judge whether @product is nil or not
          @product = @offer.product
        end
        

        【讨论】:

          猜你喜欢
          • 2011-08-24
          • 2010-12-31
          • 1970-01-01
          • 2011-07-27
          • 2011-06-17
          • 2021-08-09
          • 2023-04-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多