Ask A Question

Notifications

You’re not receiving notifications from this thread.

Get a partial to be render automatically using an ajax request? The regular request is working fine but now I want to iterate through each month without reloading the whole page

Francisco Calix asked in Javascript
show.html.erb 
 <div class="calendar_custom">
                                        
                                                <%= render partial: 'calendar',locals: {date: @day} %>
                                            </div>


show.js.erb
$("#calendar_custom").html("<%= escape_javascript render(partial: "calendar") %>");


_calendar.htmlerb
<div class="calendar_title">
        <span class="calendar_month"><%= "#{@date.strftime("%B")} #{@date.year}" %></span>
                                                    
        <%= link_to  room_path(@room,date: @date - 1.month), :class => "calendar_prev calendar_corner", :id =>"back", remote: true do %>
        <i class="lotus-icon-left-arrow"></i>
        <% end %>
                                            
        <a href="#" class="calendar_next calendar_corner"><i class="lotus-icon-right-arrow"></i></a>
</div>
                                            
            <%= calendar @date do |date| %>
            <%= date.day %>
            <% end %>
                                            


rooms_controller

  def show
    @room = Room.find(params[:id])
    @date = params[:date] ? Date.parse(params[:date]) : Date.today
    respond_to do |format|
      format.html
      format.js { render :partial => "calendar"}
  end
  end
Reply
Hey Francisco, 

Here's a basic setup to get an ajax request to render your partial

function getMonth(data, callback) {
  $.ajax({
    url: '<%= your_action_url %>',
    type: 'GET',
    data: data,
    success: callback
  });
}

I usually make a new action in the controller to handle the request, but it's not necessary:

def dates  
  @date = params[:date] ? Date.parse(params[:date]) : Date.today
end

One thing that can be a little more convoluted is if you don't know how many iterations you need to make ahead of time. So when the page first loads, do you know how many dates you'll want to go through or does it depend on something else?

If you know ahead of time, then you can just create a loop to keep running the `getDate();` function. Since you'll probably need to pass the date as a param, you can do a slight tweak as follows:


function getDate(date, data, callback) {
  $.ajax({
    url: "<%= your_action_url %> + '?date=' + date",
    type: 'GET',
    data: data,
    success: callback
  });
}

var dates = ["01-01-2018", "01-02-2018", "01-03-2018",...]
for (var i in date) {
  getDate(date);
}

*edits: looks like the normal markdown parser isn't working? Had to use the form controls instead =(
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,400+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.