Rails のスキャフォールド生成で JSON 形式をスキップする 質問する

Rails のスキャフォールド生成で JSON 形式をスキップする 質問する

次のようなコマンドを使用してレールスキャフォールドを生成する場合、rails g scaffold Thingその煩わしさを回避する方法はありますか?

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

コントローラーの中に何か入っていますか?

私は Rails のクラスを教えようとしており、まずはスキャフォールドを生成してもらいたいのですが、JSON フォーマットがすべて必要以上に複雑になっています。次のようなコントローラーを作成するスキャフォールドを生成できればもっとうれしいです。

class ThingsController < ApplicationController

  def index
    @things = Thing.all
  end

  def show
    @thing = Thing.find(params[:id])
  end

  def new
    @thing = Thing.new
  end

  def edit
    @thing = Thing.find(params[:id])
  end

  def create
    @thing = Thing.new(params[:thing])
      if @thing.save
        redirect_to @thing, notice: 'Thing was successfully created.'
      else
        render: "new" 
      end
    end
  end

  def update
    @thing = Thing.find(params[:id])
      if @thing.update_attributes(params[:thing])
        redirect_to @thing, notice: 'Thing was successfully updated.'
      else
        render: "edit" 
      end
    end
  end

  def destroy
    @thing = Thing.find(params[:id])
    @thing.destroy
    redirect_to things_url
  end
end

ベストアンサー1

jbuilderあなたのgem をコメントアウトするGemfileと、respond_toブロックは生成されません。

おすすめ記事