I'm new to Rails and was tasked to change a legacy code that's written in slim to ERB. Everything was going smoothly, but when I reached the splat attributes and found myself blocked.
The slim I'm trying to convert is like this:
doctype html
html lang="en" *data_turbolinks_messages
head
The respective controller is:
DATA_TURBOLINKS_MESSAGES = {
'data-turbolinks-messages' => ENV.fetch('TURBOLINKS_MESSAGES')
}.freeze
def data_turbolinks_messages
DATA_TURBOLINKS_MESSAGES
end
And the final HTML I've expected is like this:
<!DOCTYPE html>
<html data-turbolinks-messages="VariableValue" lang="en">
<head></head></html>
I've tried using slimrb --rails -e
but the outcome has many slim helper objects like ::Slim::Splat::Builder.new(_slim_splat_filter)
and I don't know how to write that in ERB.
Other solutions I've tried (shooting in the dark here.. again, new to Rails)
<%= **data-turbolinks-messages -%>
<%= **data-turbolinks-messages.html_safe -%>
<%= *data-turbolinks-messages %>
<%= data-turbolinks-messages.to_s %>
<% *data-turbolinks-messages.html_safe %>
<%= tag.html lang:'en' data-turbolinks-messages %>
<%= tag.html lang:'en' data: data-turbolinks-messages do %>...<% end %>
<%= tag.html lang:'en' data:{data-turbolinks-messages} do %>...<% end %>
Is there a way to bring that hash as key-value tag parameters, hopefully without changing the controller, which has other dependencies?
thanks!
Eventually I had to change the controller so the hash uses symbols as keys
DATA_TURBOLINKS_MESSAGES = {
:'data-turbolinks-messages' => ENV.fetch('TURBOLINKS_MESSAGES')
}.freeze
def data_turbolinks_messages
DATA_TURBOLINKS_MESSAGES
end
Once I did that, I could splat the hash in ERB with helper tags. I also used content_tag
instead of tag
.
<!DOCTYPE html>
<%= content_tag(:html, **data_turbolinks_messages, :lang => "pt" ) do %>
...
<% end %>
I still have no idea whether or not this is a good or bad solution, as I went through trial and error, so any new answers are welcome!