6

When I use the Rails scaffold generator to create my Rails-files, it creates among others a controller file. e.g.

rails generate scaffold potato

generates:

app/controllers/potatos_controller.rb

For my project I want this file a little more specific. E.g. I want to change this automatic generated action:

def create
  @potato = Potato.new(potato_params)

  respond_to do |format|
    if @potato.save
      format.html { redirect_to @potato, notice: 'Potato was successfully created.' }
      format.json { render :show, status: :created, location: @potato }
    else
      format.html { render :new }
      format.json { render json: @potato.errors, status: :unprocessable_entity }
    end
  end
end

to use a I18n-translation instead of the hardcoded 'Potato was successfully created.' Also I want to change some indentations, since rubocop is always complaining about it.

I have found the template of the scaffold-generator and now want to make my changes. For this I have created a file in my project: lib/templates/rails/scaffold_controller/templates/controller.rb In this file I have made my changes. (e.g. I changed the line

redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %>

to

redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} THIS IS A TEST.'" %>

But unfortunately the changes don't work. The scaffold generator still uses its own template. So what am I doing wrong here? Am I missing a step?

Update: Here is the output of the generate-command:

rails generate scaffold potato      

Running via Spring preloader in process 31479
  invoke  active_record
  ...

  invoke  scaffold_controller
  create    app/controllers/potatos_controller.rb
  ...

Screenshot of the railties:

Railtiesenter image description here

Michael B
  • 1,660
  • 3
  • 28
  • 59

4 Answers4

10

If anyone finds it useful, you can copy the default railties controller scaffold templates to your own project by running this command in your project directory:

mkdir -p lib/templates/rails/scaffold_controller && \
  cp $(bundle info railties --path)/lib/rails/generators/rails/scaffold_controller/templates/* \
  lib/templates/rails/scaffold_controller

If you use Rails 5.2 and jbuilder, you should use the jbuilder scaffolders as a base instead:

mkdir -p lib/templates/rails/scaffold_controller && \
  cp $(bundle info jbuilder --path)/lib/generators/rails/templates/* \
  lib/templates/rails/scaffold_controller
Tim Krins
  • 3,134
  • 1
  • 23
  • 25
  • When I use this method, it overrides the template as expected. However, the generated code is not the same as without the override. I appears to be the wrong template. The original code: – Bob Nicholson Nov 20 '18 at 07:16
  • When I use this method, it overrides the template as expected. However, the generated code is different. The original generated code is for both json and html formatted responses. The copied template only generates code for html responses. – Bob Nicholson Nov 20 '18 at 07:24
  • I will update this soon - the JSON and HTML controller templates are in the `jbuilder` gem. – Tim Krins Nov 20 '18 at 10:52
7

Rails 4 shows you which template is using

rails generate scaffold potato
...
invoke  scaffold_controller

You should host your modified templates in your project, i.e.
lib/templates/rails/scaffold_controller/controller.rb.

Please note that the Responders gem might change the generator used to
lib/templates/rails/responders_controller/controller.rb.

Mihai Dinculescu
  • 19,743
  • 8
  • 55
  • 70
  • Thank you for this hint. I checked the output of the generator and it sais, that it invokes `scaffold_controller` - the one that i was trying to edit. (See updated question) – Michael B Feb 11 '16 at 12:02
  • That's interesting. Can you try editting `responders_controller` too and see what happens? – Mihai Dinculescu Feb 11 '16 at 12:06
  • 1
    I have also added a screenshot if the directory structure of my rails-generators. It seems there is no *responders_controller*. I tried to create the file in my project, but it didn't have any effect on the outcome. – Michael B Feb 11 '16 at 12:13
  • My bad, `responders_controller` is defined by the Responders gem (used by Devise). You should put the modified templates in your project, i.e. `lib/templates/rails/scaffold_controller/controller.rb`. – Mihai Dinculescu Feb 11 '16 at 12:31
  • Also try without `spring`. It might not load your templates without a restart. – Mihai Dinculescu Feb 11 '16 at 12:35
  • Of course I'll change the files in my own project and not in the railties-gem itself. ;) Unfortunately stopping spring didn't help, too. – Michael B Feb 11 '16 at 12:54
  • Maybe there's a gem overwriting your template. You should debug `templates` found in `config.app_generators`. That variable holds all the paths that are checked for templates. – Mihai Dinculescu Feb 11 '16 at 13:00
1

use rake app:templates:copy to generate scaffold_controller. but, It also generate helper, controller, mailer views, controller view, helper and scaffold_controller view.

on rails github, this command located in here and it also available for not just rails 7, but rails 6, rails 5 and rails 4

0

The template to copy is here and you have to place it in lib/templates/rails/scaffold_controller/ as per Tim's answer.

balthazar
  • 407
  • 7
  • 13