【发布时间】: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