简体   繁体   中英

escape_javascript not working properly in Rails

I'm having problems with unobtrusive javascript. In my TwitterAccountController I have:

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

which is what happens when you click a button in my view named "unshare"

the unshare.js.erb that responds is:

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

It's suppose to switch the button from "unshare" button to "share" button. But when the "unshare" button is clicked, this is the code generated from 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>

you can tell the closing tags are messed up, does anyone know the reason for this???

my javascript_include_tag is:

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

the share partial:

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

I think I had the same problem and simply changed

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

for

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

which is just inverting the way you single/double quote. Worth a try :)

As i said in the comment i figured it out. I'm on rails 3.0.8 and there was a bug with escape_javascript. Updated rails and it worked fine. Thanks for the help guys!

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