24

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
VadimAlekseev
  • 2,228
  • 1
  • 18
  • 16
mattangriffel
  • 819
  • 1
  • 7
  • 18

4 Answers4

36

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

Matthias
  • 1,884
  • 2
  • 18
  • 35
e-fisher
  • 659
  • 1
  • 6
  • 8
  • 1
    Still valid in **rails 5** and also omits the generation of the `.jbuilder` views. This should be the accepted answer. – Evgeniya Manolova Dec 08 '17 at 15:55
  • 1
    Actually the correct answer (without disabling `jbuilder` at all) is to disable the generator within the Rails config. See https://stackoverflow.com/questions/22484281/skip-jbuilder-files-when-i-generate-a-scaffold – anka Apr 08 '20 at 10:48
  • so like `config.generators do |g| g.jbuilder = false end` (copied from link shared by @anka) – Jay Killeen Mar 31 '21 at 12:33
13

Just clone the file

https://github.com/rails/rails/blob/v5.2.2/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb

to your

lib/rails/generators/rails/scaffold_controller/templates/controller.rb

path in your application and customize what you want. Also, you can write your own generators for scaffolding ( http://guides.rubyonrails.org/generators.html ).

Jay Killeen
  • 2,832
  • 6
  • 39
  • 66
VadimAlekseev
  • 2,228
  • 1
  • 18
  • 16
1

I think you'd be missing an opportunity. For one thing, you'd be teaching non-standard Rails, so your students might be confused when they see the normal version in their own installations.

More importantly, the controllers are formatted that way for a reason. Rails puts an emphasis on REST, which encourages access to resources via multiple data formats. Many modern apps are de-emphasizing slower server-rendered html/erb responses in favor of json APIs. I realize this is a little over a year after your OP, and you have limited time in class, just adding some thoughts for anyone who might happen by. I think you could wave your hand over the respond_to and tell them it's setting you up for some future possibilities.

Stephen C
  • 659
  • 6
  • 9
0

You'll notice that the JSON response is coded directly into the template for the rails generator here:

https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb

I think something to note is that the scaffold generator is really intended to illustrate and moreover educate on how the Rails stack works, it shows how you can edit the controller to provide many different formats to suit your needs!

JP Silvashy
  • 46,977
  • 48
  • 149
  • 227