Search code examples
ruby-on-railsruby-on-rails-3scaffolding

Skip JSON format in rails generate scaffold


When you generate a rails scaffold using a command like rails g scaffold Thing is there any way to avoid getting that annoying

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

stuff in your controller?

I'm trying to teach a class on Rails and I'd like to start by having them generate a scaffold, but with all the json formatting it's much more complicated than it needs to be. I'd be much happier if they could generate a scaffold that created a controller like this:

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

Solution

  • Comment out gem jbuilder in your Gemfile and respond_to blocks won't be generated.