Question

Along similar lines as my last question: Issues with escaped html in .js.erb file

I'm trying to render the follow form in the rails tutorial, but I'm getting escaped html again. Are there similar tricks?

$("#<%= "follow_form#{@tag.id}" %>").html("<%=escape_javascript(render('tags/follow', :tag => @tag)) %>");

I get

<form accept-charset=UTF-8 action=/tag_user_relationships/76 class=edit_tag_user_relationship data-remote=true id=edit_tag_user_relationship_76 method=post><div style=margin:0;padding:0;display:inline><input name=utf8 type=hidden value=&#x2713; /><input name=_method type=hidden value=delete /><input name=authenticity_token type=hidden value=goedvibRxKtDRiAufp1ThWJP0rRBU2cMH2xp7qodKws= />div> <div class=actions><input id=tag_user_relationship_submit name=commit type=submit value=Unfollow />div>form>

When I just want to get a different submit button.

Code for the view:

<%= form_for current_user.tagUserRelationships.build(:tag_id => tag.id),
             :remote => true do |f| %>
  <div><%= f.hidden_field :tag_id %></div>
  <div class="actions"><%= f.submit "Follow" %></div>
<% end %>

I've continued investigating. If I add this instead:

$("#<%= "follow_form#{@tag.id}" %>").html("<%= escape_javascript(render('tags/follow', :tag => @tag)).html_safe %>");

I get the appropriate form to show up, but:

div> button div>form>

Instead of just

button

Was it helpful?

Solution

Try the following:

$("#<%= "follow_form#{@tag.id}" %>").html("<%= escape_javascript("#{render 'tags/follow'}").html_safe %>");

With the key difference being that your render call is inside the "#{}". Without it, the partial has all the single quotes removed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top