简体   繁体   中英

How to add rails html tags though Jquery?

I am trying to add rails html code using jquery, but didn't work. Code that i have tried:

   <% @user.each do |record| %>    
      <tr>
       <td>
         <%=  record.name %> 
       </td>
       <td>
         <%= record.gender %>
       </td>
      <%= record.dob %>
       </td>
    </tr>
  <% end %>
 <button id="edit_button" class="btn btn-primary btn-sm edit" onclick="editRow();">更新</button>
  

  function editrow(){
     var $item = $('.edit').closest("tr").find(".nr");
         var name = "<%= text_field 'name', class: 'form-control' %>"
         $item[0].innerHTML= name;
   } 

but it didn't work giving error like name is undefined method. also tried with different, but old value didn't displayed.

You can't dynamically insert ERB tags. The order of rendering an ERB goes something like this:

Rails reads ERB and renders HTML+assets -> 
HTML+assets get returned to your browser -> 
Your browser renders HTML and executes JavaScript

Your browser can't read ERB:(

Can you tell us a bit more about what you're trying to do with the "editrow" function, as well as show us more of the ERB?

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