When customizing rails scaffold, I struggled with output in slim, so struggle to realize it
・ 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
Rails 6.0.2.2
ruby 2.6.5
Refer to this article and add a gem called slim-rails
.
https://qiita.com/ftyabu/items/42eb62901b6b56a7dc72
Refer to this article to create a template and change its reference. https://qiita.com/akito1986/items/d9f379191fd6b98de955
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".
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? ..
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
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.
Source code in case of trouble If I noticed it early, it was solved in an instant
Recommended Posts