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 6390489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:33:15+00:00 2026-05-25T03:33:15+00:00

I want to render a partial in my js.erb file so I can use

  • 0

I want to render a partial in my js.erb file so I can use the generated HTML in my Javascript. Here’s some code to serve as an example.

create.js.erb

$(function(){
  var html = "<%= render(:partial => `pretty_box`) %>";
  $("#container").prepend(html);
});

_pretty_box.html.haml

.pretty_box_container
  .title
    Something Pretty

When create.js.erb is rendered, I get the following:

$(function(){
  var html = "<div class="pretty_box_container>
    <div class="title">
      Something Pretty
    </div>
</div>";
  $("#container").prepend(html);
});

As you would expect, this breaks my javascript. I need to strip the whitespace from the result of rendering the partial. The problem is the return value of render is an ActiveSupport::SafeBuffer object, which overrides all “unsafe” methods (see UNSAFE_STRING_METHODS), including strip. So calling render(:partial =>pretty_box).strip HTML-encodes the entire string.

I’ve tried all sorts of combinations using the methods html_safe or to_s. They don’t work because they return self, and using raw doesn’t work either because it calls to_s.html.safe.

I know I can append > and < characters to my HAML, but I don’t want to do that for every line of every partial.

  • 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-05-25T03:33:15+00:00Added an answer on May 25, 2026 at 3:33 am

    The problem is not whitespaces, but quotes.

    $(function(){
      var html = "<div class="pretty_box_container>
                             ^
                             This is where you go wrong
    

    Try this:

    $(function(){
      var html = "<%= escape_javascript(render(:partial => 'pretty_box')) %>";
      $("#container").prepend(html);
    });
    

    Check out this answer on StackOverflow for an explanation about escape_javascript.

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

Sidebar

Related Questions

I have a Javascript where I render a partial using the js.erb : $('#registration-steps').fadeOut(fast).html(<%=
In my new.html.erb page, i use the following line to render a partial and
I want to render for facebook bot special data. I have og.html.erb <!DOCTYPE html><html><head>
I use this code to generate the td tag format: <%= render(:partial => cart_item,
I have a partial called _avatar.html.erb I want to pass in an id as
render @some_object will render Rails.root/app/views/some_objects/_some_object.html.erb Now I want to handle which partial will be
In my application.html.erb layout for my app, I want to have a partial that
I have my login-partial in my application.html.erb-layout. Now I want to validate it, if
I want to render an HTML-EMail and send it to our customers using some
I want to render equations to PNG files and embed them in the HTML

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.