Ask A Question

Notifications

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/active_record/model.rb
/lib/templates/rails/scaffold_controller/controller.rb
/lib/templates/erb/scaffold/_form.html.erb.tt (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
end

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/my_models.coffee
      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 https://stackoverflow.com/questions/23624311/scaffolding-rails4-empty-controller 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.

UPDATE

https://stackoverflow.com/a/20906765/1135515

The model template needs to be in:

/lib/templates/active_record/model/model.rb
And the controller template needs to be:

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

Reply

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.

Reply
Join the discussion
Create an account Log in

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

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

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