2017-04-19 28 views
1

我注意到,通过轨道生成的默认控制器具有createif报表和update但不是在destroy为什么Rails控制器没有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 

没有如果,以防破坏语句失败

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 

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

回答

1

当使用createupdate时,有可能数据与要求不符。例如,如果您尝试向NOT NULL的列中插入零值,或者值不在由Rails validate方法指定的范围内。

在这种情况下,如果我们不能保存数据,我们要如一个新的输入表格,用户可以改正的,等等。但如果成功了,我们通常会重定向到show作出相应的反应,页。

另一方面,当使用destroy时,我们通常会从数据库中删除一行。由于这个过程不包括任何验证,所以没有(或者也许很少)关心事情会出错。所以在这种情况下,我们不需要if语句,我们可以无需谨慎。

+1

数据库中可能还有外键验证,或者模型中的'before_destroy's停止删除操作 – Iceman

+1

您是对的,删除记录时可能会有所有内容,但脚手架是默认模板。 – Fercell

+0

@Iceman是的,这是正确的,但我认为脚手架并不能解释这些情况。脚手架只关心简单的CRUD,并且如果需要更多实质性内容,则取决于编码者来改变它。 – Yaniv

相关问题