Reputation: 1699
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:
Upvotes: 7
Views: 3743
Reputation: 31
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
Upvotes: 2
Reputation: 3846
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
Upvotes: 10
Reputation: 437
The template to copy is here and you have to place it in lib/templates/rails/scaffold_controller/
as per Tim's answer.
Upvotes: 0
Reputation: 20033
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
.
Upvotes: 7