开发者

View code inside Javascript file on Rails

开发者 https://www.devze.com 2023-02-16 04:21 出处:网络
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.

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 %>
0

精彩评论

暂无评论...
验证码 换一张
取 消