29

I am working on a Ruby on Rails project and am needing to customize default views provided by Gems.

The requirement is to use Slim for template. I understand that ERB is the default template engine for Rails.

As per my observation, the priority is for ERB and if not it will use Slim/Haml views.

I am interested in knowing if it is possible to set Slim as the default instead of the ERB?

How can this be achieved so that when I create a local version of a template in Slim it will override the template provided by the gem.

Any clue will be appreciated.

Ziyan Junaideen
  • 3,270
  • 7
  • 46
  • 71

2 Answers2

36

You can use "slim-rails" gem which is built for generating slim template as default.

https://github.com/slim-template/slim-rails

Just replace gem 'slim' by gem 'slim-rails' in your Gemfile.

Billy Chan
  • 24,625
  • 4
  • 52
  • 68
  • 1
    Is there a way to add slim within `rails new MyProject`? – Besi May 23 '14 at 07:25
  • 1
    @Basi, when generating new project, there is no view so I don't think you need it. Also there is no Gemfile for adding the gem. – Billy Chan May 23 '14 at 15:43
  • 1
    You're right. However there is application.html.erb. But this is easy to change. Thanks for the comment. – Besi May 23 '14 at 15:46
  • 2
    @Besi you could achieve this with application templates - http://guides.rubyonrails.org/rails_application_templates.html – Michał Szajbe Jun 09 '14 at 20:12
  • what's the difference between "slim" and "slim-rails"? – Incerteza Mar 30 '15 at 03:15
  • @アレックス It looks like "slim-rails" has "slim" has a dependency, but has additional code for configuring Rails generators. – Eric Hu Jan 07 '16 at 12:58
23

In you Gemfile, include

gem 'slim-rails'

And to generate slim tempage, in config/application.rb add this line of code

class Application < Rails::Application
........................................

    config.generators do |g|
      g.template_engine :slim
    end
end
Amrit Dhungana
  • 4,371
  • 5
  • 31
  • 36