简体   繁体   中英

Call ruby method from JavaScript or pass parametr from view in RoR

I have view with list of websites. Details about websites are shown in retracted mode.

I need to show appropriate website details, when user clicks on some image.

I was using this post : Passing parameters to/from JavaScript & Rails

Here is my code from view:

 <table>
   <% @websites.each do |website| %>
  <%if (current_user.id == website.user_id)%>
  <tr>
    <td>  <%= link_to(image_tag("/images/caret-horizontal.png",website_path(website)) %> </td>
<td>  
<h4><%= website.name %></h4> </td>

 </tr>
 <table id="<%= website.id %>">
 <tr >
   <td >
  <td >
  <%= website.url %>
  </td>
  </td>
</tr>
 <tr id="<%= website.id %>">
<td >
<td >
<%= website.category  %>
</td>
</td>
 </tr>
 <tr id="<%= website.id %>">
 <td>
<td><%= website.language %></td>
 </td>

<%end%>

 <%end%>
 </table>

And code in application.js:

   $("#table").show();//here I need something like var table = <%= website.id%>

How I should choose appropriate website id?

or I should transmit website.id in data ?

How I can do it another way ?

There is such a great tutorial about this: http://railscasts.com/episodes/324-passing-data-to-javascript This tutorial provides you a few ways to pass data from rails to your js. :)

Anyway, change your table id to any value, and add data-id to your table

<table id="table-data" data-id="<%= website.id %>">

and now you can get the value:

var yourWebsiteId = $("#table-data").attr("data-id");

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