简体   繁体   中英

Nested quotes in link_to helper within js.erb file

I'm trying to get this to work in my votes.js.erb file I'm using for my ajax request but it's not spitting out anything.

<% if current_user && @micropost.has_evaluation?(:votes, current_user) %>
    $("#vote<%= @micropost.id %> a").html('<%= link_to "<i class='fa fa-heart fa-3x'></i>".html_safe, vote_micropost_path(@micropost, type: "down"), :method => :post, :remote => true %>')
<% else %>
    $("#vote<%= @micropost.id %> a").html('<%= link_to "<i class='fa fa-heart-o fa-3x'></i>".html_safe, vote_micropost_path(@micropost, type: "up"), :method => :post, :remote => true %>')
<% end %>

It has to do with the nested quotations but I can't find the right combo. I've been trying different things like using \\ to escape the inner quotes and also &quot; but this doesn't seem to work for me either with this this code

<% if current_user && @micropost.has_evaluation?(:votes, current_user) %>
    $("#vote<%= @micropost.id %> a").html("<%= escape_javascript(link_to '<i class=&quot;fa fa-heart fa-3x&quot;></i>'.html_safe, vote_micropost_path(@micropost, type: 'down'), :method => :post, :remote => true %>")
<% else %>
    $("#vote<%= @micropost.id %> a").html("<%= escape_javascript(link_to '<i class=&quot;fa fa-heart-o fa-3x&quot;></i>'.html_safe, vote_micropost_path(@micropost, type: 'up'), :method => :post, :remote => true %>")
<% end %>

Try

<% if current_user && @micropost.has_evaluation?(:votes, current_user) %>
    $("#vote<%= @micropost.id %> a").html('<%= link_to "<i class=\"fa fa-heart fa-3x\"> </i>".html_safe, vote_micropost_path(@micropost, type: "down"), :method => :post, :remote => true %>')
<% else %>
    $("#vote<%= @micropost.id %> a").html('<%= link_to "<i class=\"fa fa-heart-o fa-3x\"></i>".html_safe, vote_micropost_path(@micropost, type: "up"), :method => :post, :remote => true %>')
<% end %>

or use

    $("#vote<%= @micropost.id %> a").html('<%= link_to content_tag('i', class: 'fa fa-heart fa-3x'), vote_micropost_path(@micropost, type: "down"), :method => :post, :remote => true %>')

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