简体   繁体   中英

Ruby on Rails - Adding content generated by JavaScript to view


I am trying to add some graphs generated by my JavaScript(generate_graph.js.erb) to a view.
I am using rails 3.2.1
So in the view I have -

index.html.erb

<table>
<tr>
  <th>Description</th>
  <th>chart</th>
</tr>

<%= render @stats %>

partial _stat.html.erb

<tr>
  <td><%= stat.description %></td>
  <td> javascript_fn(stat.data)</td>
</tr>

So the questions are
1. Where should I put generate_graph.js.erb
2. How to call this js so as to add the graph to the "td"

Thanks for reading !!

Is there a reason you've made the generate_graph file a .js.erb instead of just .js?

If it will work as a regular js file, put it in app/assets/javascripts.

Then just do something like:

<tr>
    <td><%= stat.description %></td>
    <td>
        <script>
           javascript_fn(<%= stat.data %>);
        </script>
    </td>
</tr>

I know some people prefer not to have inline javascript, but sometimes it's just easier, imo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM