[RUBY] I tried customizing slim with Scaffold

What is this

Overview

When customizing rails scaffold, I struggled with output in slim, so struggle to realize it

For people like this

・ People who use the rails template engine as slim Or anyone who wants to make the rails template engine slim ・ People who customize scaffold but give up on view customization Or someone who wants to customize scaffold

environment

Rails 6.0.2.2 ruby 2.6.5

Preparation for implementation

Part 1 Make the template engine slim

Refer to this article and add a gem called slim-rails. https://qiita.com/ftyabu/items/42eb62901b6b56a7dc72

Part 2 Make it possible to customize the scaffold template

Refer to this article to create a template and change its reference. https://qiita.com/akito1986/items/d9f379191fd6b98de955

Steps to realization

Part 1 Change the directory name from erb to slim

I think that the following files have been created in "Part 2" above. (May be different depending on the settings)

$ tree lib/templates
lib/templates
├── erb
│   ├── controller
│   │   ├── templates
│   │   │   └── view.html.erb
│   │   └── view.html.erb
│   ├── mailer
│   │   ├── templates
│   │   │   ├── view.html.erb
│   │   │   └── view.text.erb
│   │   ├── view.html.erb
│   │   └── view.text.erb
│   └── scaffold
│       ├── _form.html.erb
│       ├── edit.html.erb
│       ├── index.html.erb
│       ├── new.html.erb
│       ├── show.html.erb
│       └── templates
│           ├── _form.html.erb
│           ├── edit.html.erb
│           ├── index.html.erb
│           ├── new.html.erb
│           └── show.html.erb
└── rails
    ├── assets
    │   ├── javascript.js
    │   ├── stylesheet.css
    │   └── templates
    │       ├── javascript.js
    │       └── stylesheet.css
    ├── controller
    │   ├── controller.rb
    │   └── templates
    │       └── controller.rb
    ├── helper
    │   ├── helper.rb
    │   └── templates
    │       └── helper.rb
    └── scaffold_controller
        ├── api_controller.rb
        ├── controller.rb
        └── templates
            ├── api_controller.rb
            └── controller.rb

16 directories, 28 files

Let's change this "erb" directory to "slim".

Part 2 Rewrite the file from erb.tt specifications to slim.tt specifications

Well, here is the production. I searched for a helpful article, but couldn't find it. First, let's take a look at the contents of the target file

lib/templates/erb/scaffold/index.html.erb.tt

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

<h1><%= plural_table_name.titleize %></h1>

<table>
  <thead>
    <tr>
<% attributes.reject(&:password_digest?).each do |attribute| -%>
      <th><%= attribute.human_name %></th>
<% end -%>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %>
      <tr>
<% attributes.reject(&:password_digest?).each do |attribute| -%>
        <td><%%= <%= singular_table_name %>.<%= attribute.column_name %> %></td>
<% end -%>
        <td><%%= link_to 'Show', <%= model_resource_name %> %></td>
        <td><%%= link_to 'Edit', edit_<%= singular_route_name %>_path(<%= singular_table_name %>) %></td>
        <td><%%= link_to 'Destroy', <%= model_resource_name %>, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <%% end %>
  </tbody>
</table>

<br>

<%%= link_to 'New <%= singular_table_name.titleize %>', new_<%= singular_route_name %>_path %>

I wasn't sure about the <%% = symbol, maybe it's familiar to people who use erb on a regular basis.

https://qiita.com/ykhirao/items/7ff9ce515a82a6dd24ea This article introduced <%% =. This is to prevent the erb file from being corrupted even if the erb is expanded once.

How can I achieve that with slim? ..

Part 3 (Failure) Prove the theory that if you try to hit the gem for the time being

I suddenly remembered here. Existence of ʻerb2 slim`.

I tried it, believing that "erb was written correctly and would fix it". ..

lib/templates/erb/scaffold/index.html.slim

p#notice
  - %= notice
h1
  = plural_table_name.titleize
table
  thead
    tr
      - attributes.reject(&:password_digest?).each do |attribute|
        th
          = attribute.human_name
      th[colspan="3"]
  tbody
    |  <ruby code="% @
    = plural_table_name
    | .each do |
    = singular_table_name
    | | "> 
    tr
      - attributes.reject(&:password_digest?).each do |attribute|
        td
          - %= <%= singular_table_name
          | .
          = attribute.column_name
          |  %>
      td
        - %= link_to 'Show', <%= model_resource_name
        |  %>
      td
        - %= link_to 'Edit', edit_<%= singular_route_name
        | _path(
        = singular_table_name
        | ) %>
      td
        - %= link_to 'Destroy', <%= model_resource_name
        | , method: :delete, data: { confirm: 'Are you sure?' } %>
    - % end
br
- %= link_to 'New <%= singular_table_name.titleize
| ', new_
= singular_route_name
| _path %> 

It only smells of collapse. .. .. Lol As expected, when I executed scaffold, it was a series of syntax errors. After all it seems more certain to fix it by yourself

Part 4 Get hints from slim-rails

However, I wanted to get started with something, and when I was researching various things, suddenly "Slim-rails should be playing with scaffold templates in the first place." I had a good idea, so I checked it. Then ... https://github.com/slim-template/slim-rails was.

slim-rails/lib/generators/slim/scaffold/templates/index.html.slim

h1 Listing <%= plural_table_name %>

table
  thead
    tr
<% attributes.each do |attribute| -%>
      th <%= attribute.human_name %>
<% end -%>
      th
      th
      th

  tbody
    - @<%= plural_table_name %>.each do |<%= singular_table_name %>|
      tr
<% attributes.each do |attribute| -%>
        td = <%= singular_table_name %>.<%= attribute.name %>
<% end -%>
        td = link_to 'Show', <%= singular_table_name %>
        td = link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>)
        td = link_to 'Destroy', <%= singular_table_name %>, data: { confirm: 'Are you sure?' }, method: :delete

br

= link_to 'New <%= human_name %>', new_<%= singular_table_name %>_path

In other words, you can refer to this and change the template to your liking. If you find it, this is the one. Please live a wonderful customized life while changing the wording, changing the structure, and experimenting diligently.

Conclusion

Source code in case of trouble If I noticed it early, it was solved in an instant

Recommended Posts

I tried customizing slim with Scaffold
I tried UPSERT with PostgreSQL.
I tried BIND with Docker
I tried using JOOQ with Gradle
I tried morphological analysis with MeCab
I tried to interact with Java
I tried UDP communication with Java
I tried GraphQL with Spring Boot
I tried Flyway with Spring Boot
I tried using Realm with Swift UI
I tried to get started with WebAssembly
I tried using Scalar DL with Docker
I tried using OnlineConverter with SpringBoot + JODConverter
I tried time-saving management learning with Studyplus.
I tried playing with BottomNavigationView a little ①
I tried using OpenCV with Java + Tomcat
I tried Lazy Initialization with Spring Boot 2.2.0
I tried to implement ModanShogi with Kinx
I tried Spring.
I tried tomcat
I tried youtubeDataApi.
I tried refactoring ①
I tried JHipster 5.1
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
I tried to manage struts configuration with Coggle
I also tried WebAssembly with Nim and C
I made blackjack with Ruby (I tried using minitest)
I tried to break a block with java (1)
I tried Getting Started with Gradle on Heroku
I tried what I wanted to try with Stream softly.
[I tried] Spring tutorial
I tried to implement file upload with Spring MVC
I tried to read and output CSV with Outsystems
I tried to implement TCP / IP + BIO with JAVA
I tried running Autoware
I tried using Gson
[Java 11] I tried to execute Java without compiling with javac
[Rails] I tried playing with the comment send button
I started MySQL 5.7 with docker-compose and tried to connect
I tried QUARKUS immediately
I tried using TestNG
[Machine learning] I tried Object Detection with Create ML [Object detection]
I tried Spring Batch
I tried using Galasa
I tried to draw animation with Blazor + canvas API
I tried OCR processing a PDF file with Java
I played with Refinements
I tried node-jt400 (Programs)
I tried node-jt400 (execute)
I tried node-jt400 (Transactions)
roman numerals (I tried to simplify it with hash)
I tried installing Ruby on Rails related plugin with vim-plug
I tried to make an introduction to PHP + MySQL with Docker
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
I tried to increase the processing speed with spiritual engineering
[Rails] I tried to create a mini app with FullCalendar
Spring Boot Introductory Guide I tried [Accessing Data with JPA]
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I tried automatic deployment with CircleCI + Capistrano + AWS (EC2) + Rails