Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple Ajax Requests on one Rails View
    text
    copied!<p>I have two ajax requests on one page (one displaying a date, one displaying a calendar)</p> <p>Request one : on click of a date (on the calendar), will update the date.</p> <p>Request two : you can change between months of the calendar.</p> <p>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.</p> <p>Can I make it so it only executes one function of index.js.erb at a time?</p> <p><strong>Index.js.erb</strong></p> <pre><code>$("#calendar").html("&lt;%= escape_javascript(render('layouts/calendar')) %&gt;"); $("#today").html("&lt;%= escape_javascript(render('layouts/today')) %&gt;"); </code></pre> <p><strong>_today.html.erb</strong></p> <pre><code>&lt;div class="span3 offset2 dateToday"&gt; &lt;% end %&gt; &lt;span id="currentdate"&gt; &lt;%= @eventDate.strftime("%B") %&gt;&lt;br&gt; &lt;span id="dayDate"&gt;&lt;%= h @eventDate.day %&gt;&lt;/span&gt; &lt;/span&gt; &lt;/div&gt; </code></pre> <p><strong>_calendar.html.erb</strong></p> <pre><code> &lt;h2 id="month"&gt; &lt;span class="pull-left"&gt;&lt;%= link_to "&lt;", :month =&gt; (@date.beginning_of_month-1).strftime("%Y-%m-01") %&gt;&lt;/span&gt; &lt;%= h @date.strftime("%B %Y") %&gt; &lt;span class="pull-right"&gt;&lt;%= link_to "&gt;", :month =&gt; (@date.end_of_month+1).strftime("%Y-%m-01") %&gt;&lt;/span&gt; &lt;/h2&gt; &lt;%= calendar_for(@events, :year =&gt; @date.year, :month =&gt; @date.month) do |t| %&gt; &lt;%= t.head('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun') %&gt; &lt;%= t.day(:day_method =&gt; :eventdate) do |date, events| %&gt; &lt;div class="day"&gt; &lt;%= link_to(date.day, {:day =&gt; date }, :remote =&gt; true, :class =&gt; "btn dayBtn") %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p><strong>EDIT: routes.rb</strong></p> <pre><code>resources :calendar root :to =&gt; "calendar#index" match '/faq', to: 'calendar#faq' match ':controller/:action' </code></pre> <p><strong>EDIT - link to ajax action</strong></p> <pre><code>&lt;%= link_to "&lt;", :month =&gt; (@date.beginning_of_month-1).strftime("%Y-%m-01"), :action =&gt; "update_calendar", :remote =&gt; true %&gt; </code></pre> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload