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
show.html.erb
<div class="calendar_custom">
<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">
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
Hey Francisco,
Here's a basic setup to get an ajax request to render your partial
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:
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 =(