2017-04-19 1 views
1

私はレールによって生成されたデフォルトのコントローラはifcreate内の文とupdateではなくdestroyRailsコントローラが、破棄が成功したかどうかを確認するif文を持っていないのはなぜですか?

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

createまたはupdateを使用すると、データが要件と一致しない可能性があります。たとえば、カラムにゼロ値をNOT NULLで挿入しようとした場合や、値がRails validateメソッドで指定された範囲内にない場合。そのようなユーザーが修正を行うことができ、新たな入力フォームなどのように、これらのインスタンスで

、我々はデータを保存に失敗した場合、我々はそれに応じて対応したい、しかし、成功した場合、我々は通常、showにリダイレクトページ。

destroyを使用している場合は、通常、データベースから行を削除しています。このプロセスには検証が含まれていないため、事態が悪化する懸念はありません。したがって、このような状況では、ifステートメントは不要であり、慎重に処理を進めることができます。

+1

データベース内に外部キーの検証が残っているか、またはモデルの 'before_destroy'が削除操作を停止している可能性があります。 – Iceman

+1

あなたが正しいですが、レコードの削除中にすべてがあるかもしれませんが、足場はデフォルトのテンプレートです。 – Fercell

+0

@Icemanはい、それは正しいですが、私は足場がそのような状況を説明しているとは思わないのです。足場は単なるCRUDに関係しているだけであり、もっと重要なことがあればそれを変更するためにコーダーに依存しています。 – Yaniv

関連する問題