Loading a Single Partial in Rails After the Page has Rendered

In my application, I have a partial page called _dashboard.html.erb. Within the dashboard, there are multiple calls to render different partials. One of these is the _calendar.html.erb partial, which retrieves events from Google Calendar and displays them. However, this process slows down the loading time of the entire dashboard. I would like the _calendar partial to load after the rest of the dashboard has finished loading, without requiring any click event, but rather when the page initially loads.

pages_controller.rb

# Currently on shift
def jump
@title = "#{User.find(session[:user_id]).firstname}'s Dashboard"
@tasks = Task.where(:completed => 0)
@questions = Question.where(:answered => 0)


serv = GCal4Ruby::Service.new
serv.authenticate("email","pass")
cal = GCal4Ruby::Calendar.find(serv, {:title => "Skidmore Center for Sex and Gender Relations"})
#@events = GCal4Ruby::Event.find(serv, {'start-min' => Time.now.utc.xmlschema, 'start-max' => 5.days.from_now.utc.xmlschema })
@events = GCal4Ruby::Event.find(serv, {'start-min' => Time.parse("01/01/2011").utc.xmlschema, 'start-max' => Time.parse("06/01/2011").utc.xmlschema, :calendar => cal.id})

end

The 'jump' method is where I render the dashboard partial. Here's how I call the calendar partial in /app/views/layouts/_dashboard.html.erb:

<div id="main-content">
  <div class="post">
    <h3>Calendar</h3>
    <%= render 'layouts/calendar' %>
  </div>
</div>

And here is /app/views/layouts/_calendar.html.erb:

<% @events.each do |event| %>
  <% if event.start_time >= Time.now && event.start_time <= 5.days.from_now %><li><%= event.start_time.to_s(:short) %> <%= event.title %></li><% end %>
<% end %>

I'm familiar with AJAX but lack experience implementing it in Rails or independently. Any assistance provided in simpler terms would be greatly appreciated!

Thank you

Update: I tried using the async_partial gem, but encountered issues with the partial fetching events from Google Calendar. The partial functions correctly without the async_partial.

Answer №1

To address this issue without relying on a gem, a common approach is to utilize an event handler for the page load event. For example, using jQuery:

// This code will run once jQuery becomes available, typically when 
// the document is fully loaded.
$(function () {

    // Swap out the existing calendar content with the section generated by your partial
    $("#your-calendar-div-id").replaceWith('<%= escape_javascript(render 'layouts/calendar') %>');
    
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Displaying a timer output in an HTML div every second using PHP

I created a custom PHP countdown timer that displays the remaining time in hours, minutes, and seconds (specifically 3 hours) - named countdown.php. The timer output is displayed within a <div> tag on another page called index.html. Now, I am lookin ...

What is the best way to implement Ajax in Rails 3.1?

It's common knowledge that Rails 3.1 utilizes CoffeeScript and JQuery. In the past, I would handle AJAX requests by responding to a template named some_action.js.erb Now with CoffeeScript available, I want my templates to be able to use CoffeeScript ...

How about incorporating AJAX into your HTML code?

I am currently in the process of developing an email system for a company that wishes to use it for sending emails. To achieve this, I have implemented a custom HTML editor for creating email content. My goal is to post the email contents to an external PH ...

Guide to implementing jQuery autocomplete with an AJAX request to a PHP file

I'm currently working on implementing a jQuery autocomplete feature, and here is the code I have so far: $( "#text" ).autocomplete({ source: function( request, response ) { $.ajax({ type: 'GET', url: ' ...

What is the best way to modify the glyphicon within the anchor tag of my AJAX render property?

Consider the use of Ajax: "target 0" with a render option for an anchor tag. Initially, I am utilizing the class "glyphicon glyphicon-chevron-right". Now, I wish to change it to "glyphicon glyphicon-chevron-down" when clicked. $(document).ready(funct ...

Avoid displaying identical items when rendering a page from JSON data

I am using ajax and json to render a page. The structure of my json is as follows: {"status":"ok","rewards":[{"id":201,"points":500},{"id":202,"points":500}]}. I want to load the data using ajax only once if 'points' have duplicates in any of the ...

reloading a webpage while keeping the current tab's URL selected

I want to incorporate a feature I saw on the jQuery website at http://jqueryui.com/support/. When a specific tab is pressed, its contents should open. However, when the page is refreshed, the same tab should remain open. I'm trying to implement this i ...

Exploring the optimal SEO strategies for website creation using PHP programming

In my recent work, I've delved into the world of SEO and it's been quite an eye-opening experience. Admittedly, I've always been a bit skeptical about SEO, thinking it was just buzz-word nonsense with no real substance. I've always beli ...

Utilizing AJAX to showcase an HTML popup

I am currently working on a project to create an HTML page that will display another HTML file in an alert when a button is pressed. However, I am facing an issue where the content is not being displayed as expected. <html> <head> ...

The function $.ajax may not be available, but rest assured that the jquery library is functioning properly

Upon checking the console, I noticed the following: Here is the AJAX code snippet in use: $("#accept").click(function(){ id = $("#idInput").val(); password = $("#passwordInput").val(); alert(id) alert(password) $.aja ...

Experiencing a 400 Bad Request error while attempting an Ajax call to a Controller in ASP.NET

Having trouble making a simple request from a CSHTML page to a Controller in my ASP.NET MVC website application. Attempting a DELETE call using Ajax: $.ajax({ url: "/company/delete", type: "DELETE", success: function (e) { showAlert(&a ...

update confirmed data from a record

I am facing a unique challenge, where I have an edit form that updates a record in a table. One of the fields, let's say username, needs to be unique. To validate this, I am using the jQuery validation plugin along with the remote method as shown belo ...

Execute a script when a post is loaded on a WordPress page

I am looking to have my jQuery script execute every time a post page is opened or loaded. I tried using echo in the script but it did not work. Where should I place the script to ensure it runs? single.php <script src="https://ajax.googleapis.com/aja ...

I'm encountering an error when trying to pass multiple parameters in an AJAX request

I am trying to pass three parameters to my ajax code. Here is the snippet of my code: $(document).ready(function () { SearchText(); }); function SearchText() { $("#txt712").autocomplete({ source: function (request, resp ...

Displaying various Ajax html responses

The function $('.my-button').click(function(e) is designed to display the output of the MySQL query in display.php, presented in HTML format. While it functions correctly, since each button is looped for every post, the script only works for the ...

Challenges with CJuiDialog pop-up feature

As a beginner in AJAX, jQuery and similar technologies, I am currently working on a project that involves creating a calendar where substitutes can indicate their availability for specific days and time periods. However, I am facing an issue with implement ...

What is the best method for transferring data from a Facebook application to my server using Ajax?

(Given that FBML and FBJS are no longer supported by Facebook, I am unable to utilize those methods for transmitting data to my server). I have created an IFRAME Facebook application, primarily designed for use on pages. When a page administrator accesse ...

Obtaining additional information through ajax in Yii2

I have encountered an issue where, while receiving data from an ajax call, I am also getting unnecessary data such as all the contents of my PHP files. The problem is illustrated in the image below. How can I resolve this issue? https://i.stack.imgur.com ...

Removing a feature through AJAX in CodeIgniter

Issue Resolved.. I have updated my code accordingly.. Thanks to everyone for the help Description: My code displays an array of message traces (each message is displayed in a grey panel). Users can delete unwanted messages by clicking on the delete button ...

Refreshing div with updated form/content using jQuery and AJAX

Is it possible to use jQuery/AJAX functions to replace a form (or any content) with another form when a button is clicked, without reloading the page? Essentially creating a multi-part form dynamically. Can I achieve this with the following code snippet? ...