[Ruby] how to start slim

1 minute read

slim installation

Install slim in the app folder.

gem install slim

Now it seems that slim files with the extension .html.slim will be converted as .html.erb that you usually use.

gem install html2slim

With this, it seems that it will be possible to convert from html.erb → html.slim.

bundle exec erb2slim app/views app/views

If you get an error with this, add the following two statements to the gemfile and install bundle.

gem'slim-rails'
gem'html2slim'
bundle install

This will delete the erb file that was originally in the view folder. *If there is no file with the extension .erb, skip it.

bundle exec erb2slim app/views app/views -d

In order to automatically create a slim file in the future, OK if you specify slim in config in config/application.rb as follows.

config/application.rb


module App
  class Application <Rails::Application
    config.generators.template_engine = changed to :slim #slim
  end
end

Preparation before displaying #slim file

rails g controller tweets

Inside the view folder, there is a tweets folder. In it, create a new file called index.html.slim. Write something in the file for easy understanding.

views/tweets/index.html.slim


Hello, world!

Add an index action in the controller.

controllers/tweets_controller.rb


class ArchivesController <ApplicationController
def index
end
end

Next is routing.

config/routes.rb


Rails.application.routes.draw do
  root "tweets#index"
end

Restart the server and view the page

Don’t forget to restart the server. If you do not restart, you will get an error such as “ArchivesController#index is missing a template for request formats: text/html”.

http://localhost:3000/

Reference article

  • https://qiita.com/ngron/items/c03e68642c2ab77e7283
  • https://teratail.com/questions/149418