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

  • Home
  • SEARCH
  • 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 3609982
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:41:44+00:00 2026-05-18T21:41:44+00:00

When writing a helper for printing javascript that can be used from both other

  • 0

When writing a helper for printing javascript that can be used from both other helpers and views, I stumbled upon the following problem:

def javascript(print_tag = false, &block)
  content_for(:javascript) do
    if print_tag
      javascript_tag(&block)          # does not work
      javascript_tag { block.call }   # does work 
    else
      capture(&block)
    end
  end
end

This helper should be called with javascript { "alert('hurray'); }.

In the first alternative – which I expected to work – the Rails javascript_tag helper renders an empty <script type="text/javascript"> //<![CDATA[ //]]> </script> tag.

The second alternative, however, works as expected.

What’s going on there? How can that be different?

  • 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-18T21:41:45+00:00Added an answer on May 18, 2026 at 9:41 pm

    You say you are doing this on your views, right?

    <%= javascript { "alert('hurray');" } %>
    

    But for content_tag(&block) to work, you should call javascript the way content_tag is intended to be used in views, which is:

    <% javascript do %>
      alert('hurray');
    <% end %>
    

    content_tag‘s behavior is different depending on where it’s called from, see the function block_called_from_erb? in the source code. In the first case this function returns true because the block does come from an erb (and then it’s concated, you don’t want that!), in the second returns false (you re-created the block from scratch) and content_tag simply returns the string content, which is what you want.

    # ./action_view/helpers/javascript_helper.rb
    tag = content_tag(:script, javascript_cdata_section(content), html_options.merge(:type => Mime::JS))
    if block_called_from_erb?(block)
      concat(tag)
    else
      tag
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an ASP.NET MVC Html Helper which basically takes 2 HTML Helpers that
I'm writing a helper class and I want it to be used in both
I'm writing C# code that uses the windows IP Helper API. One of the
I am writing a gem which provides helpers for views. The HTML I want
I am writing a helper that adds an HTML attribute to a link_to tag
I have found that writing PHP code within classes can become rather long: $this->parser->parse_syntax($this->get_language_path($this->language),
I am writing helper classes for a large project in PHP and I have
I am writing a Browser Helper Object for ie7, and I need to detect
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
I'm currently writing a helper function in VB.NET to convert an array of enum

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.