Ruby on rails ROR脚手架破坏重定向到
实际上,我构建了一个参考用户设计和产品的“报价”框架。我可以在特定产品页面上添加报价。然而,我意识到,当我试图删除一个报价时,它在默认情况下会重定向到products_url。如何将其重定向回特定的产品页面?当我创建注释时,它会将_重定向到特定的产品页面。删除不这样做。 我试过使用 原始代码Ruby on rails ROR脚手架破坏重定向到,ruby-on-rails,ruby,path,destroy,Ruby On Rails,Ruby,Path,Destroy,实际上,我构建了一个参考用户设计和产品的“报价”框架。我可以在特定产品页面上添加报价。然而,我意识到,当我试图删除一个报价时,它在默认情况下会重定向到products_url。如何将其重定向回特定的产品页面?当我创建注释时,它会将_重定向到特定的产品页面。删除不这样做。 我试过使用 原始代码 class OffersController < ApplicationController before_action :set_offer, only: [:show, :edit, :updat
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
这实际上表明了我的错误。26实际上是提供的id。它实际上应该重定向到。它向我展示了提取的源代码,如下所示
Couldn't find Product with 'id'=26
def set_product
@product = Product.find(params[:id])
end
我不确定我是否理解了这个问题,但我认为您只需要将产品id作为附加参数传递,例如:
= link_to 'destroy', offer_path(@offer, product_id: @product.id), method: :delete
然后在你的控制器中使用
redirect_to product_path(params[:product_id])
在销毁方法中执行此操作 Product=@offer.Product 重定向到:产品 @您使用的产品未设置。所以我们需要在这里设置产品标识。
这就是为什么我们通过关系从offer变量中获取产品id,您在set_product中所做的只是使用params[:id]来查找产品,但是当您调用destroy时,params[:id]是指offer_id,这就是为什么您会得到RecordNotFoundError。我想你可以写这个
def set_product
# maybe you should judge whether @product is nil or not
@product = @offer.product
end
def set_product
# maybe you should judge whether @product is nil or not
@product = @offer.product
end