escape_javascript在Rails中无法正常工作

时间:2011-10-19 00:58:57

标签: javascript ruby-on-rails

我遇到了不引人注目的javascript问题。在我的TwitterAccountController我有:

def unshare
  @twitter_account = TwitterAccount.find(params[:id])
  #@twitter_account.update_attribute(:message_enabled, true)
  respond_to do |format|
    format.html { redirect_to @twitter_account.user }
    format.js  # responds to TwitterAccount/share.js.erb
  end
end

当您点击名为“unshare”

的视图中的按钮时会发生这种情况

响应的unshare.js.erb是:

var div_name = '<%="div#share_form_#{@twitter_account.id}"%>'
$(div_name).html("<%= escape_javascript(render(:partial => 'shared/share', :locals => {:twitter_account    => @twitter_account}))%>");

假设将按钮从“取消共享”按钮切换到“共享”按钮。但是当单击“取消共享”按钮时,这是从escape_javascript生成的代码:

<form accept-charset=UTF-8 action=/twitter_accounts/1/share data-remote=true method=get>
  <div style=margin:0;padding:0;display:inline><input name=utf8 type=hidden value=&#x2713; />
  div>        
  <input name=commit type=submit value=share />
form>

你可以告诉结束标签搞砸了,有没有人知道这个的原因???

我的javascript_include_tag是:

<%= javascript_include_tag 'jquery.min.js', "rails.js", "d3.js","d3.time.js", "raphael.js", "collapse_menu.js" %>
分享部分:

<%= form_tag(share_twitter_account_path(twitter_account), :method => 'get', :remote => true) do %>
  <%= submit_tag "share"%>
<% end %>

2 个答案:

答案 0 :(得分:3)

我认为我遇到了同样的问题并且只是改变了

$(div_name).html("<%= escape_javascript(render(:partial => 'shared/share', :locals => {:twitter_account    => @twitter_account}))%>");

$(div_name).html('<%= escape_javascript(render(:partial => "shared/share", :locals => {:twitter_account    => @twitter_account}))%>');

这只是颠倒你单/双引号的方式。值得一试:))

答案 1 :(得分:0)

正如我在评论中所说,我想出来了。我在rails 3.0.8上有一个与escape_javascript有关的错误。更新了rails并且工作正常。谢谢你的帮助!