js.erb文件中link_to帮助器中的嵌套引号

时间:2014-04-17 13:45:20

标签: javascript ruby-on-rails ajax

我试图在我的votes.js.erb文件中使用此功能我正在使用我的ajax请求,但它没有吐出任何东西。

<% 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 %>

它与嵌套引用有关但我无法找到合适的组合。 我一直在尝试使用\来逃避内部引号以及&quot;等不同的事情,但这对我来说似乎不适用于此代码

<% 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 %>

1 个答案:

答案 0 :(得分:1)

尝试

<% 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 %>

或使用

    $("#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 %>')