Amit Vashistha
Amit Vashistha

Reputation: 260

Special Character & is getting printed as &

In my index.html.erb page when data is queried from MSSQL it get displayed as Me&Mine. I tried this:

$("#3").text("<%= escape_javascript(raw render(:partial => "var") %>");

but it doesn't work.

index.html.erb :

<%= link_to "Link", {:action => "AjaxView",:col => "colname"}, 
        :update => "Ajaxcall", :remote => true %>
<tr>
<td id="#3" style="height:25px">data displayed here</td>

Controller:

def AjaxView
  @vars= Var.find(:all,:conditions => { :varName=> "one" },:select=>(params[:col]))
  respond_to do |format|
    format.js { render :layout=>false }
  end
end

AjaxView.js.erb:

 if ( @col.to_s == "colName") {
   $("#3").text("<%= escape_javascript(render(:partial => "var") %>");
 }
 elsif ( @col.to_s == "colName1")
 {
   $("#2").text("<%= escape_javascript(render(:partial => "var") %>");
 }

_var.html.erb:

<%= var.col1 %>

How can I display Me&Mine instead of Me&amp;Mine?

Upvotes: 3

Views: 3508

Answers (2)

Akhil Gautam
Akhil Gautam

Reputation: 169

Convert this:

$("#3").text("<%= escape_javascript(raw render(:partial => "var") %>");

to:

$("#3").html("<%= escape_javascript(raw render(:partial => "var") %>");

and it will work fine.

This is because when you are using "text" which is plain text without any formatting, so it is not able to render "&" and prints its raw form. But using HTML will render it with markup/formatting.

Upvotes: 0

Michael Kohl
Michael Kohl

Reputation: 66847

Have you tried using html_safe on it?

Upvotes: 7

Related Questions