Rails escapes the quote in link_to_function partial

I have a small piece of rails code that allows users to enter an overview. If already written, it displays the current overview with a link to edit it.

      <div id="text_area">
        <% if @user_rating.review.blank? %>
          <%= render :partial => "reviews/text_area" %>
        <% else %>
          <%= simple_format @user_rating.review %>
          <%= link_to_function "Edit Review" do |page|
            page.replace_html :text_area, :partial => "reviews/text_area"
          end %>
        <% end %>
      </div>

      

This works correctly EXCEPT if the overview contains a double quote character. Here is the HTML output it generates after the user has already written the review (the else block on top).

      <div id="text_area">
          <p>some text with "quotation marks"</p>
          <a href="#" onclick="try {
Element.update(&quot;text_area&quot;, &quot;&lt;form action=\&quot;/pages/iphone-reviews/reviews/new/write\&quot; class=\&quot;edit_rating\&quot; id=\&quot;edit_rating_16\&quot; method=\&quot;post\&quot;&gt;&lt;div style=\&quot;margin:0;padding:0\&quot;&gt;&lt;input name=\&quot;_method\&quot; type=\&quot;hidden\&quot; value=\&quot;put\&quot; /&gt;&lt;input name=\&quot;authenticity_token\&quot; type=\&quot;hidden\&quot; value=\&quot;fSZK30kpWEUtCxOvr4xDF1O+puDiPm87mhaxaLirIT8=\&quot; /&gt;&lt;/div&gt;\n        &lt;textarea cols=\&quot;40\&quot; id=\&quot;rating_review\&quot; name=\&quot;rating[review]\&quot; rows=\&quot;20\&quot; style=\&quot;width:100%;height:150px;\&quot;&gt;some text with &quot;quotation marks&quot;&lt;/textarea&gt;\n&lt;/form&gt;&quot;);
} catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.update(\&quot;text_area\&quot;, \&quot;&lt;form action=\\\&quot;/pages/iphone-reviews/reviews/new/write\\\&quot; class=\\\&quot;edit_rating\\\&quot; id=\\\&quot;edit_rating_16\\\&quot; method=\\\&quot;post\\\&quot;&gt;&lt;div style=\\\&quot;margin:0;padding:0\\\&quot;&gt;&lt;input name=\\\&quot;_method\\\&quot; type=\\\&quot;hidden\\\&quot; value=\\\&quot;put\\\&quot; /&gt;&lt;input name=\\\&quot;authenticity_token\\\&quot; type=\\\&quot;hidden\\\&quot; value=\\\&quot;fSZK30kpWEUtCxOvr4xDF1O+puDiPm87mhaxaLirIT8=\\\&quot; /&gt;&lt;/div&gt;\\n        &lt;textarea cols=\\\&quot;40\\\&quot; id=\\\&quot;rating_review\\\&quot; name=\\\&quot;rating[review]\\\&quot; rows=\\\&quot;20\\\&quot; style=\\\&quot;width:100%;height:150px;\\\&quot;&gt;some text with &quot;quotation marks&quot;&lt;/textarea&gt;\\n&lt;/form&gt;\&quot;);'); throw e }; return false;">Edit Review</a>

      </div>

      

This example breaks off because the overview text contains a double quote. It works with single quotes and any other text I can think of. I think rails doesn't avoid double quote correctly in javascript.

The error thrown in Firebug is this:

missing) after the list of arguments [Break this this error] \ "id = \" rating_review \ "name = \" rating [re ... n: right; \ "> \ n

I don't understand how to fix this. Thanks!

0


a source to share


2 answers


Found a solution that worked. I'm not really sure why it worked, but it did.

A few times above, I call a partial render of the text area and form where the user can edit their view, like

<%= render :partial => "reviews/text_area" %>

      

All I had to do was go in and edit this partial. One of the lines in the form looked like this:



<%= f.text_area :review %>

      

All I did was change it to this:

<textarea cols="40" id="rating_review" name="rating[review]" rows="20"><%=@user_rating.review%></textarea>

      

Basically just place it manually so it doesn't use the rails feature to create the text area. For some reason, it works now. Maybe something that could be improved in the rails text_area function.

+1


a source


You can write a javascript helper:

def escape_javascript(javascript)
        javascript.gsub!("\r\n", "\\n")
        javascript.gsub!(/\n|\r/, "\\n")
        javascript.gsub(/["']/) {|m| "\\#{m}"} || javascript
end

      



and give it a name when you save the review.

0


a source







All Articles