shwashbuckle
shwashbuckle

Reputation: 303

Overriding default scaffold views in rails not working

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?

Cheers, Leigh.

Upvotes: 1

Views: 1157

Answers (3)

Dan
Dan

Reputation: 1995

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
...

Upvotes: 3

shwashbuckle
shwashbuckle

Reputation: 303

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...

Upvotes: 0

Vasseurth
Vasseurth

Reputation: 6486

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!

Upvotes: 1

Related Questions