Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7820305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:14:57+00:00 2026-06-02T07:14:57+00:00

Consider this: <% str = http://domain.com/?foo=1&bar=2 %> Now these cases: <%=str%> # output:http://domain.com/?foo=1&amp;bar=2 <%=str.html_safe%>

  • 0

Consider this:

<%
str = "http://domain.com/?foo=1&bar=2"
%>

Now these cases:

<%=str%>
# output:http://domain.com/?foo=1&amp;bar=2

<%=str.html_safe%>
# output:http://domain.com/?foo=1&bar=2

<%="#{str.html_safe}"%>
# output:http://domain.com/?foo=1&amp;bar=2

<%=""+str.html_safe%>
# output:http://domain.com/?foo=1&amp;bar=2

I need to output the URL with other strings. How can I guarantee that the ampersand will be unescaped? For reasons beyond my control I can’t send &amp;.

Please help! Pulling my hair here :\

EDIT: To clarify, I actually have an array like so:

@images = [{:id=>"fooid",:url=>"http://domain.com/?foo=1&bar=2"},...]

I am creating a JS array (the image_array var) to use in my app this way:

image_array.push(<%=@images.map{|x|"{id:'#{x[:id]}',url:'#{x[:url].html_safe}'}"}.join(",")%>);

This generates:

image_array.push({id:'fooid',url:'http://domain.com/?foo=1&amp;bar=2'},...);

Which does not work in my specific case. I need the url without the amp; part.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:14:58+00:00Added an answer on June 2, 2026 at 7:14 am

    When you write:

    "#{foo.bar}"
    

    this is ~equivalent to writing

    foo.bar.to_s
    

    So what you are actually doing is:

    <%=str.html_safe.to_s%>
    

    …which Rails no longer sees as being safe, and so it hits your resulting string with a round of HTML escaping.

    I don’t know the internals of Rails, but I assume that the html_safe method extends the string object with an instance variable flagging it as OK, but when you wrap that in another string via interpolation you are getting a new string without that flag.

    Edit: To answer your needs, use raw or call html_safe on your final string:

    <%=raw "foo#{str}"%>
    <%="foo#{str}".html_safe%>
    

    or in your case:

    image_array.push(<%=raw @images.map{…}.join(',')%>);
    image_array.push(<%=@images.map{…}.join(',').html_safe%>);
    

    See also this question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this url: www.anysite.com/get_count/?sex=5&sex=6&city=5&city=7&job=7&job=8...... For using in dynamic query in view: model = anyModel.objects.filter(sex=5).filter(sex=6).filter(city=5)....
Consider this one piece of Perl code, $array[$x]->{foo}->[0]= January; I analyze this code as
I'm confused. Consider this code working the way I expect: >>> foo = u'Émilie
Consider this code: class Foo { public void doIt(String... strs) { System.out.println(this is varargs);
Consider this class: class foo(object): pass The default string representation looks something like this:
This is elementary I guess: Let's consider this snippet: for i in range(3): sys.stdout.write(str(i)
Consider this example: #include <algorithm> #include <iostream> int main() { std::string str = abcde4fghijk4l5mnopqrs6t8uvwxyz;
consider the program below char str[5]; strcpy(str,Hello12345678); printf(%s,str); When run this program gives segmentation
Consider this line of JavaScript str += onclick=runTest( + field + , + atom[0]
Consider this JavaScript code snippet: Object.prototype.log = function() { // here, you have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.