Ruby on rails 为什么Rails控制器没有if语句来检查销毁是否成功?

Ruby on rails 为什么Rails控制器没有if语句来检查销毁是否成功?,ruby-on-rails,controller,Ruby On Rails,Controller,我注意到rails生成的默认控制器在create和update中有if语句,但在destroy def update @product = Product.find(params[:id]) respond_to do |format| if @product.update_attributes(params[:product]) format.html { redirect_to @product, notice: 'Product was s

我注意到rails生成的默认控制器在
create
update
中有
if
语句,但在
destroy

  def update
    @product = Product.find(params[:id])

    respond_to do |format|
      if @product.update_attributes(params[:product])
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end
如果销毁失败,则不使用if语句

  def destroy
    @product = Product.find(params[:id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to products_url }
      format.json { head :no_content }
    end
  end
这是脚手架控制器的其余部分

class ProductsController < ApplicationController
  # GET /products
  # GET /products.json
  def index
    @products = Product.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products }
    end
  end

  # GET /products/1
  # GET /products/1.json
  def show
    @product = Product.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @product }
    end
  end

  # GET /products/new
  # GET /products/new.json
  def new
    @product = Product.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @product }
    end
  end

  # GET /products/1/edit
  def edit
    @product = Product.find(params[:id])
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(params[:product])

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

  # PUT /products/1
  # PUT /products/1.json
  def update
    @product = Product.find(params[:id])

    respond_to do |format|
      if @product.update_attributes(params[:product])
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product = Product.find(params[:id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to products_url }
      format.json { head :no_content }
    end
  end
end
class ProductsController

为什么Rails控制器默认情况下在destroy方法中没有IF语句?

当使用
create
update
时,数据可能不符合要求。例如,如果您试图在列中插入一个nil值,并且
不为NULL
,或者该值不在Rails
validate
方法指定的范围内

在这些情况下,如果我们无法保存数据,我们希望做出相应的响应,例如使用新的输入表单,用户可以在其中进行更正等。但是如果成功,我们通常会重定向到
显示
页面


另一方面,当使用
destroy
时,我们通常从数据库中删除一行。因为这个过程不包括任何验证,所以没有(或者,可能很少)担心事情会出错。因此,在这种情况下,我们不需要if语句,我们可以不加警告地继续操作。

数据库中可能仍然存在外键验证,或者在模型中销毁前停止删除操作。
s您是对的,删除记录期间可能会有所有内容,但scaffold是默认模板。@Iceman Yes,这是正确的,但我不认为脚手架能解释这些情况。脚手架只涉及简单的积垢,如果需要更为实质性的东西,则需要程序员对其进行修改。@Yaniv是的,我明白你的观点,尽管我不一定完全同意。但知道这个决定背后的真正原因会很有趣,因为如果我没记错的话,它从远古时代起就几乎没有变化,至少Rails 2。