rinold simon
rinold simon

Reputation: 3052

Using "link_to" how to get html and rails code in same line

 <%= link_to 'Received Messages<span class="badge badge info">
 @receivedmessage.count</span>'.html_safe, '/messages/show' %>

below is the output i'm getting so far. I have to get the "message count number" inside the badge instead of "@receivedmessage.count"

enter image description here

Upvotes: 0

Views: 409

Answers (2)

Kevin
Kevin

Reputation: 1223

When you want to interpolate your variable you should use quotes instead of single quotes. Also, you'll need to use #{} around your variable. An example:

name = 'John'
puts "Hello, #{name}!"

Notice how I've used quotes on the second line when using string interpolation and surrounded the variable name with #{}?

Now, let's apply it to your example:

<%= link_to "Received Messages <span class='badge badge info'>#{@receivedmessage.count}</span>".html_safe, '/messages/show' %>

Update

String interpolation and html_safe might not always be a good idea, but since your only interpolating a count it should be fine. A better option would be to use what Micha has suggested (using link_to ... do):

<%= link_to '/messages/show' do %>
  Received Messages <span class="badge badge info"><%= @receivedmessage.count %></span>
<% end %>

Upvotes: 0

Mischa
Mischa

Reputation: 43298

You can use link_to ... do, which IMO is cleaner than newmediafreak's answer:

<%= link_to '/messages/show' do %>
  Received Messages<span class="badge badge info"><%= @receivedmessage.count %></span>
<% end %>

Upvotes: 3

Related Questions