Lee Eather
Lee Eather

Reputation: 355

How can I pass back a jquery string from a js.erb in rails

I have this code in a create.js.erb file which I expect back from a comment form I am subitting with remote: true

<div class="comment">
  <%= @comment.content %>
  <%= link_to "delete", comment_path(@comment, :deletecomment => 
      "delete"), remote: true, method: :delete, data: { confirm: "You 
      sure?" } 
  %>
</div>

It seems that rails only processes valid jquery in this file. I can only make my code with the '' around the html to make it a jquery string then send it back like this but funny enough the results when checking the data returned is a string with the single quotes as part of the string.

I then use html = data.replace(/\'/g, ""); to remove the single quotes but is there a better way to do this?

For instance I cannot comment in the js.erb any notes I want to add in there when normally you can comment out the js.erb with /*...*/

I was thinking of removing the remote: true from the form I am using and doing a normal ajax call upon submit and setting dataType as html but still I think before this rails will have already corrupted my string.

Upvotes: 0

Views: 297

Answers (1)

Anand
Anand

Reputation: 6531

Make a partial :- _example_partial.html.erb

<div class="comment">
   <%= comment.content %>
   <%= link_to "delete", comment_path(comment, :deletecomment => "delete"),remote: true, method: :delete, data: { confirm: "You sure?" } %>
</div> 

and in create.js.erb

$("#id_of_div_where_the_partial_will_append").append("<%= j render 'example_partial', comment: @comment%>");

Upvotes: 1

Related Questions