Reputation: 130
I need to show dynamic records in a bootstrap tooltip. I am able to use HTML in bootstrap tooltip but getting error when I use Ruby code. My code sample is as follows
"<a data-toggle='tooltip' data-placement='right' data-html='true' data-original-title='
<h5>Title for #{label} file</h5>
<% users.each do |user| %>
<div class=row>
<div class=col-sm-6>
#{user.name}
</div>
<div class=col-sm-6>
<div class=rectangle id=#{status}>
#{user.id)}
</div>
</div>
</div>
<%end%>'
href='/link'>Right tooltip</a>"
Everything works fine in this code if users.each
loop is not used.
If it is used I'm getting the error
NameError: undefined local variable or method 'user'.
While users have all records in it. The issue is it is not recognizing Ruby syntax.
Upvotes: 1
Views: 436
Reputation: 130
ERB.new("<a data-toggle='tooltip' data-placement='right' data-html='true' data-original-title='
<h5>Title for #{label} file</h5>
<% users.each do |user| @user = user %>
<div class=row>
<div class=col-sm-6>
<%= @user.name %>
</div>
<div class=col-sm-6>
<div class=rectangle id=#{@user.status}>
<%= @user.id %>
</div>
</div>
</div>
<%end%>'
href='/link'>Right tooltip</a>").result(binding)
This is the working code.
Upvotes: 1
Reputation: 230461
and that method gets called in view as a value of
<td>
That looks like this, I imagine?
<td><%= decorator.bootstrap_html %></td>
So you have to pass it through ERB yourself. Something like this:
<td><%= ERB.new(decorator.bootstrap_html).result(binding) %></td>
Or probably it's the decorator that should do this. Just make sure your users
is in the scope and replace #{user.name}
with a proper ERB's <%= user.name %>
.
Upvotes: 1