1
votes

I am wanting to override the default scaffold views for my rails 4 app. I have copied all of the erb scaffold files from railties and placed them in the following folder under my app /lib/templates/erb/scaffold.

I modified the new.erb.html file so that I could tell if the lib/templates erb file is being used to generate the view instead of the default railties file.

After running the command:

rails g scaffold Customer code:string name:string

and reloading the new customer page it doesn't pick up my customised scaffold file to generate the new customer view.

Is there something else I need to set within the app to tell it to look at the lib/templates folder for scaffold template files?

  • Rails version is: Rails 4.1.4
  • OS version is: Ubuntu 14.04 LTS
  • Ruby version is: ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]

Cheers, Leigh.

3

3 Answers

3
votes

I had the same problem save for the fact that my changes were never showing up no matter how many times I destroyed or generated my scaffold entity. To fix it, I added the following defaults to my config/application.rb file (see Customizing Your Workflow at Ruby on Rails Guides):

...
class Application < Rails::Application
...
  config.generators do |g|
    g.orm             :active_record
    g.template_engine :erb
    g.test_framework  :test_unit, fixture: true
  end
end
...
1
votes

I'll link to you this post which has the answer. But in short what they say is: You need to add this code in a file which declares your engine.

class Engine < Rails::Engine

  config.app_generators do |g|
    g.templates.unshift File::expand_path('../templates', __FILE__)
  end

end

It also links to examples which do what you seem to be looking to do. Enjoy!

0
votes

I found a "solution" however I'm not entirely sure why re-running the scaffold command didn't produce the same results. Instead I ran: rails destroy scaffold Customer and then re-ran: rails g scaffold Customer code:string name:string and it worked. The big question will be if I make additional view template changes will I need to do the same again or will re-running the scaffold command work. Time will tell...