Reputation: 2728
I want to define the width of a status bar dynamically.
First I define the variable:
<% target = (clicks/total_clicks)*100 %>
Then I try to embed the ruby code into a css width tag:
<div class="bar" style="width: <%= target %>%">
However, the html output looks like this
<div class="progress progress-striped">
<div class="bar" style="width: 0%"></div>
</div>
I tried to convert the target var as a string:
<% target = ((clicks/total_clicks)*100).to_s %>
I obviously also tried to assign a not-dynamic value as well:
<% target = 40.to_s %>
But with no success.
How can I embed a ruby var into a CSS style tag? Thanks for any help.
Upvotes: 2
Views: 1974
Reputation: 23503
You are getting 0 because of how you are using the numbers. You are using Integers
. Do this:
<%= target = (Float(10)/Float(20))/100 %>
Using floats will allow you to get the percentage you are using. When you use Integers, it will generally round the number off. Hope this helps.
EDIT:
You can also use the .to_f
method as well, instead of Float
Upvotes: 2