1

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 %>
5
  • 1
    I assume the partial itself is good? Commented Oct 19, 2011 at 1:06
  • partial is good. it's been tested without javascript already. what quotes am i missing? Commented Oct 19, 2011 at 1:10
  • i added the share partial. im still unclear what attributes values is to be quoted. Commented Oct 19, 2011 at 2:04
  • What's the real response from the server? I mean, not the HTML generated, but the JS it will execute... Commented Oct 19, 2011 at 3:48
  • okay after researching 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! Commented Oct 19, 2011 at 4:07

2 Answers 2

3

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 :)

Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.