View code inside Javascript file on Rails
previously i had javascript code right in the top of my view and it would work. Then, i learned that you can include the js file using yield and i did开发者_如何转开发 just that.
I had rails view code inside my javascript code like :
$( "#exp-progressbar" ).progressbar({
value: <%= ((@quest.end_time - Time.now).to_i * 100 ) / (@quest.duration * 60) %>
});
Now, after i do it with yield, i get an error when i do that in the separate js file. How can i do it now ?
I found the solution :
<% content_for :javascript_includes do %>
<% if not @quest.nil? %>
<script type="text/javascript">
var experience = <%= ((@quest.end_time - Time.now).to_i * 100 ) / (@quest.duration * 60) %>;
var quest_duration = <%= @quest.duration * 60 %>;
var quest_timer = <%= (@quest.end_time - Time.now).to_i %>;
</script>
<%= javascript_include_tag "experience.js" %>
<% end %>
<% end %>
<% if flash[:error] %>
<%= flash[:error] %><br><br>
<% end %>
精彩评论