0

I'm using the wice-grid gem.

Their examples show the views in .erb but I'd like to use .slim for my project but am having trouble converting the example erb into working slim.

# ERB renders and works perfectly
<%= grid(@tasks_grid) do |g|

  g.column do |task|
    task.id
  end

  g.column  do |task|
    task.title
  end
end -%>
# Converted SLIM does not render properly
- grid(@tasks_grid) do |g|
  - g.column do |task|
    = task.id

  - g.column  do |task|
    = task.title

I've tried using this erb2slim converter but it outputs end which slim doesn't allow.

Edit: Solution (thanks to the help below!)

# Generate grid
- grid = grid(@tasks_grid) do |g|
  - g.column do |task|
    - task.id

  - g.column  do |task|
    - task.title

# Render content in a safe way
= content_tag(:div, grid)
yzi0148
  • 45
  • 1
  • 6

1 Answers1

1
= grid(@tasks_grid) do |g|
  - g.column do |task|
    - task.id

  - g.column  do |task|
    - task.title

= is equal to <%= in ERB templates. It outputs the expression. - evaluates but does not output like <% in ERB. If you look at the erb template you can see that it just outputs the return value of grid(@tasks_grid) do not task.id.

Also note that converters rarely do the job to 100% and are not a replacement for knowing both the original and target language. They just provide a starting point to make the process less ardous.

max
  • 96,212
  • 14
  • 104
  • 165
  • The ERB template has `<%=` outputting the entire block for `grid(@tasks_grid)`. I've tried changing the slim version to: – yzi0148 Feb 01 '20 at 10:38
  • Yeah sorry that first character should have been =. I have edited the answer. – max Feb 01 '20 at 10:40
  • Thank you! I think it's almost there. After converting to the example above, I'm able to render a string that contains the html for the table. To get it to actually display as html, I did `- table_output = grid(@tasks_grid) do |g|` then after the block, rendered it: `= table_output.html_safe`. It's a bit more convoluted than I'd like but manageable. Do you have any suggestions on a cleaner way to do this? – yzi0148 Feb 01 '20 at 10:59
  • 1
    In slim you can't just call a method on `end` like you would in ERB so thats probally the cleanest way to do it in the template. You could write a method that wraps `grid` and calls `super.html_safe`. But I don't really get why you are calling .html_safe on the whole thing. And that's probally not the best idea in the first place. https://makandracards.com/makandra/2579-everything-you-know-about-html_safe-is-wrong – max Feb 01 '20 at 11:13