I would like to create a table similar to the following as helper using content_tag.

Asked 2 years ago, Updated 2 years ago, 36 views

When creating a table in rails such as the following,
I always implement it using if statements.

*It is written in slim

categories/index.html.slim


- if store.categories.present?
  - store.categories.each do | category |
      tr
        td = category.id
        td = category.name
        td = link_to 'edit', new_store_category_path(store.id)
- else
  td
  td
  td = link_to 'New', new_store_category_path(store.id)


Now I've created the table I intended.
It's good when the number of td is small like this time, but if you want to create a table with nearly 10 td numbers,
The readability will be very poor.

I wanted to refactivate these things, so I was looking into them.

In my investigation, I found out that it might be possible to use a helper using content_tag, so I tried various things using sample code using content_tag.

However, I'm in trouble because it didn't work out.

The goal is to refactivate tables that can be used in multiple branches with these if statements.

Thank you for your cooperation.

ruby-on-rails ruby

2022-09-30 15:46

2 Answers

I can't write the code in the comments, so I have a question for the respondent.
I wanted to refactoring, but I didn't know the goal that I was looking for widely.
If you have the following awareness of the problem, I think it would be better to summarize it by repeating it.

If you create a table with nearly 10 td's, the readability will be considerably poor.

-if store.categories.present?
  - store.categories.each do | category |
    tr
      td = category.id
      td = category.name
      td = link_to 'edit', new_store_category_path(store.id)
- else
  - 2.times.each do 
    td
  td = link_to 'New', new_store_category_path(store.id)

There are two, so I think I can use 10.times for 10 pieces.
If this is the only problem, I can answer it simply, so could you please add more specific images, pseudo codes, etc.?


2022-09-30 15:46

You can use the String multiplication operator to write succinctly without having to provide helper:

-if store.categories.present?
  - store.categories.each do | category |
      tr
        td = category.id
        td = category.name
        td = link_to 'edit', new_store_category_path(store.id)
- else
  == content_tag(:td)*2
  td = link_to 'New', new_store_category_path(store.id)

The number of iterations only changes the number multiplied (two in the above).
Note, however, that you are using the == notation (equivalent to raw helper).

If you really want to help, you can do the method as it is.

helpers/table_helper.rb

moduleTableHelper
  default_td(count=1)
    raw(content_tag(:td)*count)
  end
end

views/categories/index.html.slim

-if store.categories.present?
  - store.categories.each do | category |
      tr
        td = category.id
        td = category.name
        td = link_to 'edit', new_store_category_path(store.id)
- else
  = empty_td(2)
  td = link_to 'New', new_store_category_path(store.id)


2022-09-30 15:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.