0

https://github.com/slim-template/slim-rails/issues/168

How could I convert below erb to slim?

ERB code

<div class="click" <%= love_type ? "data-review=#{target}" : "data-recommend=#{target}" %>
                                   data-logined='<%= sth%>'></div>

I tried...

.click[data-logined="#{sth}"
      love_type ? data-review="#{target}" : data-recommend="#{target}"]

and

.click data-logined="#{sth}" love_type ? data-review="#{target}" : data-recommend="#{target}"

But not work...

How could I do this writing simple DRY code?

Nima
  • 3,309
  • 6
  • 27
  • 44
Crazy Pioneer
  • 33
  • 1
  • 10

1 Answers1

0

Try this:

.click *{ data: { logined: "#{sth}", review: "#{love_type ? target : nil }", recommend:"#{love_type ? nil : target}" } }

Or you can use a helper method to return data as a hash:

.click *{ data: as_a_hash(love_type) }

helper_method:

def as_a_hash(args)
  { logined: "#{sth}", review: "#{args ? target : nil }", recommend:"#{args ? nil : target}" }
end
Emu
  • 5,763
  • 3
  • 31
  • 51