All threads / Using custom scaffold templates for Rails models and controllers?

Ask A Question


You’re not receiving notifications from this thread.

Using custom scaffold templates for Rails models and controllers?

Dan Tappin asked in Rails

After getting tired of rebuilding all my default scaffold models, views and controllers I decided to create custom templates as outlines on various blogs / SA questions etc.

I created:

/lib/templates/erb/scaffold/ (and so on for the actions etc.)

I added this to my config/application.rb file:

config.generators do |g|
      g.orm             :active_record
      g.template_engine :erb
      g.test_framework  :test_unit, fixture: true
      g.skip_routes  true

I then run the scaffold:

rails g scaffold MyModel name description notes:text flag_active:boolean slug company_id:integer
Running via Spring preloader in process 52960
      invoke  active_record
      create    db/migrate/20200130165523_create_my_models.rb
      create    app/models/my_model.rb
      invoke    test_unit
      create      test/models/my_model_test.rb
      create      test/fixtures/my_models.yml
      invoke  resource_route
       route    resources :my_models
      invoke  inherited_resources_controller
      create    app/controllers/my_models_controller.rb
      invoke    erb
      create      app/views/my_models
      create      app/views/my_models/index.html.erb
      create      app/views/my_models/edit.html.erb
      create      app/views/my_models/show.html.erb
      create      app/views/my_models/new.html.erb
      create      app/views/my_models/_form.html.erb
      invoke    test_unit
      create      test/controllers/my_models_controller_test.rb
      create      test/system/my_models_test.rb
      invoke    helper
      create      app/helpers/my_models_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/my_models/index.json.jbuilder
      create      app/views/my_models/show.json.jbuilder
      create      app/views/my_models/_my_model.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/
      invoke    scss
      create      app/assets/stylesheets/my_models.scss

The issues are that the model and controller templates are not used and the views work fine.

I found this post detailing that active admin (which I use) uses inherited_resources_controller. I am guessing that is part of the issue but I am at a loss how to fix this. I can't seem to find an obvious solution posted anywhere else.


The model template needs to be in:

And the controller template needs to be:

My updated question would be then why is rails using inherited_resources_controller vs scaffold controller and can I fix that?

I stumbled on half of the solution - if I put my controller template in inherited_resources_controller that seems to fix the controller template issue.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 35,095+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.