Steven Ritchie
Steven Ritchie

Reputation: 228

Multiple Ajax Requests on one Rails View

I have two ajax requests on one page (one displaying a date, one displaying a calendar)

Request one : on click of a date (on the calendar), will update the date.

Request two : you can change between months of the calendar.

What happens is I have both functions in index.js.erb and it always executes both, resetting the date in 'request one' everytime I change months.

Can I make it so it only executes one function of index.js.erb at a time?

Index.js.erb

$("#calendar").html("<%= escape_javascript(render('layouts/calendar')) %>");
$("#today").html("<%= escape_javascript(render('layouts/today')) %>");

_today.html.erb

<div class="span3 offset2 dateToday">
    <% end %>
    <span id="currentdate">
        <%= @eventDate.strftime("%B") %><br>
        <span id="dayDate"><%= h @eventDate.day %></span>
    </span>
</div>

_calendar.html.erb

  <h2 id="month">
      <span class="pull-left"><%= link_to "<", :month => (@date.beginning_of_month-1).strftime("%Y-%m-01") %></span>
      <%= h @date.strftime("%B %Y") %>
      <span class="pull-right"><%= link_to ">", :month => (@date.end_of_month+1).strftime("%Y-%m-01") %></span>
  </h2>
  <%= calendar_for(@events, :year => @date.year, :month => @date.month) do |t| %>
      <%= t.head('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun') %>
      <%= t.day(:day_method => :eventdate) do |date, events| %>
  <div class="day">
      <%= link_to(date.day,  {:day => date }, :remote => true, :class => "btn dayBtn") %>      
  </div>
  <% end %>

EDIT: routes.rb

resources :calendar

root :to => "calendar#index"

match '/faq',   to: 'calendar#faq'
match ':controller/:action'

EDIT - link to ajax action

<%= link_to "<", :month => (@date.beginning_of_month-1).strftime("%Y-%m-01"), :action => "update_calendar", :remote => true %>

Thanks!

Upvotes: 3

Views: 2113

Answers (1)

rovermicrover
rovermicrover

Reputation: 1453

You are either going to have to set up logic to respond differntly based on some params you pass along, or you need to break these out into two differnt actions, because they really are two differnt actions.

So something like this if you use the param approach.

-if param[:one]
  $("#calendar").html("<%= escape_javascript(render('layouts/calendar')) %>");
-if param[:two]
  $("#today").html("<%= escape_javascript(render('layouts/today')) %>");

Or as said before just break them up into different actions.

In your controller

def update_today
 #your today logic thats in your current action
 respond_to do |format|
   format.js{render}
 end
end

def update_calendar
 #your today logic thats in your current action
 respond_to do |format|
   format.js{render}
 end
end

You would then have to add routes for these two actions.

update_today.js.erb

$("#today").html("<%= escape_javascript(render('layouts/today')) %>");

update_calendar.js.erb

$("#calendar").html("<%= escape_javascript(render('layouts/calendar')) %>");

Upvotes: 4

Related Questions