Rails Slim

Slim

Slim is template language which works with Rails3, 4.
It makes view codes simple.

Install and Apply your project

Add following description to your project Gemfile

# Slim
gem 'slim-rails'

You can use it after running bundle install

How to change from erb to slim

File name is

Example

<p id="notice"><%= notice %></p>

<h1>Listing Products</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Description</th>
      <th>Price</th>
      <th>Discontinued</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @products.each do |product| %>
      <tr>
        <td><%= product.name %></td>
        <td><%= product.description %></td>
        <td><%= product.price %></td>
        <td><%= product.discontinued %></td>
        <td><%= link_to 'Show', product %></td>
        <td><%= link_to 'Edit', edit_product_path(product) %></td>
        <td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Product', new_product_path %>
p#notice = notice

h1 Listing Products For Slim

table
  thead
    tr
      th Name
      th Description
      th Price
      th Discontinued
      th colspan="3"
  
  tbody
    - @products.each do |product|
      tr
        td = product.name
        td = product.description
        td = product.price
        td = product.discontinued
        td = link_to 'Show', product
        td = link_to 'Edit', edit_product_path(product)
        td = link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' }
    
    
br

= link_to 'New Product', new_product_path

If there are erb and slim at the same directory,
.erb is prior.
So if you want to use slim, please delete same name erb file.

html2slim

html2slim is a tool to convert from html to slim, erb to slim.
Install it and we can use html2slim and erb2slim
Install command is

gem install html2slim

How to convert?

erb2slim show.html.erb show.html.slim

This is not perfect, so we need to check whether this is converted correctly or not. But it’s useful tool.

Ref

Rails Webook