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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:48:37+00:00 2026-05-24T18:48:37+00:00

I would like to replace a set of div each time I receive an

  • 0

I would like to replace a set of div each time I receive an ajax request (the website I am working on is full ajax…). For example, it could be checking the number of messages in an inbox, or displaying user statistics…

The thing is I don’t wan’t to repeat this call to my rendering function many times in my code.

I tried to call my function in before_filter, but since I call render :update, it doesn’t work the second time.

I tried to create my own function render_page in application_controller :

def render_page
  render :update do |page|
    yield(page)
    # page.replace_html :div, ...
  end
end

But somehow the context seem to be lost : when i call render_page on a controller’s function, I can’t access helper functions…

Thanks !

  • 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-24T18:48:37+00:00Added an answer on May 24, 2026 at 6:48 pm

    Found it !

    I dug a little into the ruby on rails documentation to find out how the render :update function works.

    First, I saw that render :update was simply calling update_page by sending the code block…

    http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#method-i-update_page

    And this function is calling the constructor of JavaScriptGenerator by sending view_context (which is simply the instance of ActionView::Base).

    JavaScriptGenerator.new(view_context, &block).to_s.html_safe
    

    http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/new/class

    And in the constructor of JavaScriptGenerator, we can observe

    def initialize(context, &block) #:nodoc:
      @context, @lines = context, []
      include_helpers_from_context
      @context.with_output_buffer(@lines) do
        @context.instance_exec(self, &block)
      end
    end
    

    instance_exec is a ruby function that allows to call a block within a context… That was exactly what I needed.

    So, the solution (or at least one working solution…) is to define render_page in application_controller :

    def render_page(&block)
      render :update do |page|
        page << 'console.log("before_code");'
        self.instance_exec(page, &block)
        page << 'console.log("after_code");'
      end
    end
    

    This way, instead of calling in my controllers

    render :update do |page|
      page.replace_html ...
      helper_functions...
    end
    

    I call

    render_page do |page|
      page.replace_html ...
      helper_functions...
    end
    

    And I am still allowed to call my helper functions (since the context has been passed)…

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

Sidebar

Related Questions

I would like to replace the default malloc at link time to use a
I would like to set the width and the height of the div element
I have set a button onTouchListener and I would like to replace it with
I would like to replace every line that starts with a certain expression (example:
I would like to remove a set of suffixes from a set of full
I would like to replace only the group in parenthesis in this expression :
I would like to replace the entire header area in an NSIS installer with
I would like to replace drop down list (that has on change event that
I would like to replace the first character ' x ' with the number
I would like to replace blank spaces/white spaces in a string with commas. STR1=This

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.