building html from string

47 views Asked by At

Really, I lost my whole day on building proper html, rendered as js,

 <tr>
    <td><%= c.club_name.capitalize %></td>
    <td><%= c.full_address %></td>
    <td>
      <%= link_to player_path(c), :"data-no-turbolink" => true, target: "_blank" do %>
         <span class="glyphicon glyphicon-play-circle"></span>
      <% end %>
    </td>
    <td>
      <%= link_to edit_club_path(c) do %>
           <span class="glyphicon glyphicon-pencil"></span>
      <% end %>
     </td>
     <td>
        <%= link_to c, method: :delete, data: { confirm: 'Are you sure?' } do %>
            <span class="glyphicon glyphicon-trash glyphicon-red"></span>
        <% end %>
     </td>
  </tr>  

and the content for show.js.erb which is rendered as html is as:

var name = <%= @club.club_name.capitalize %>;
var addr = <%= @club.full_address %>;

var link_player = <%= link_to player_path(@club), :"data-no-turbolink" => true, target: "_blank" do %>
              '<span class="glyphicon glyphicon-play-circle">     </span>'.html_safe
                <% end %>
var link_edit = <%= link_to edit_club_path(@club) do %>
                        '<span class="glyphicon glyphicon-pencil"></span>'.html_safe
                    <% end %>
var link_delete = <%= link_to @club, method: :delete, data: { confirm: 'Are you sure?' } do %>
                        '<span class="glyphicon glyphicon-trash glyphicon-red"></span>'.html_safe
                    <% end %>

$('#club_add').modal_success();
<% flash[:notice] = "Club created successfully" %>
var out_data = $.parseHTML("<tr><td>"+name+"</td><td>"+addr+"</td><td>"+link_player+"</td><td>"+link_edit+"</td><td>"+link_delete+"</td></tr>");
$('#clubs_table_tbody').prepend(out_data);  

I'm getting js syntax error, how to build the first code or string as proper html to be rendered in browser ?

0

There are 0 answers