Hi Everyone,

I am having a problem getting Rails to render a page when it is called
from javascript/ajax. I posted a detailed question on StackOverflow but
no one has answered yet.(Rails 5 will not render POST made from javascript)

Basically, I am doing a very simple ajax POST (also tried GET with same
results), the equivalent of: localhost:3000/lockout/2016-03-16.

The ajax request gets past routes.rb, and calls the appropriate method
in the controller. The controller processes it correctly (database is
updated, debug print statements show up in log, etc.) but it does NOT
render.

When I submit the same request manually from the browser (as a GET) it
again is processed correctly, but then it DOES render.

In all cases the log shows this (with times slightly different):

/Started POST "/lockout/2016-03-16" for 127.0.0.1 at 2016-03-06 13:05:22
-0500//
//Processing by LockoutsController#toggle as HTML//
/*
*
*
/Rendered lockouts/toggle.html.erb within layouts/application (0.6ms)//
//Completed 200 OK in 244ms (Views: 203.0ms | ActiveRecord: 11.4ms)/

In fact, the log shows identical entries from the original /Started
GET/POST/ line for all attempts at invoking the method, but only when
the URL is entered manually will the view render.

BTW, the ajax is called from fullcalendar's dayClick event if that
helps. fullcalendar's eventClick events DO render.

Does anyone have any ideas?

Thanks,

Don Z

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/56DC7555.3080501%40ziesig.org.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Colin Law at Mar 6, 2016 at 7:58 pm

    On 6 March 2016 at 18:22, Donald Ziesig wrote:
    Hi Everyone,

    I am having a problem getting Rails to render a page when it is called from
    javascript/ajax. I posted a detailed question on StackOverflow but no one
    has answered yet. (Rails 5 will not render POST made from javascript)

    Basically, I am doing a very simple ajax POST (also tried GET with same
    results), the equivalent of: localhost:3000/lockout/2016-03-16.

    The ajax request gets past routes.rb, and calls the appropriate method in
    the controller. The controller processes it correctly (database is updated,
    debug print statements show up in log, etc.) but it does NOT render.

    When I submit the same request manually from the browser (as a GET) it again
    is processed correctly, but then it DOES render.

    In all cases the log shows this (with times slightly different):

    Started POST "/lockout/2016-03-16" for 127.0.0.1 at 2016-03-06 13:05:22
    -0500
    Processing by LockoutsController#toggle as HTML
    *
    *
    *
    Rendered lockouts/toggle.html.erb within layouts/application (0.6ms)
    Completed 200 OK in 244ms (Views: 203.0ms | ActiveRecord: 11.4ms)

    In fact, the log shows identical entries from the original Started GET/POST
    line for all attempts at invoking the method, but only when the URL is
    entered manually will the view render.

    BTW, the ajax is called from fullcalendar's dayClick event if that helps.
    fullcalendar's eventClick events DO render.

    Does anyone have any ideas?
    Can you show us the code you are you using to do the post?

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsGhCkS62BXdo58xMsmyhkY%2B02gOapatqFqetywWxjcbg%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • Donald Ziesig at Mar 6, 2016 at 9:45 pm

    On 03/06/2016 02:57 PM, Colin Law wrote:
    On 6 March 2016 at 18:22, Donald Ziesig wrote:
    Hi Everyone,

    I am having a problem getting Rails to render a page when it is called from
    javascript/ajax. I posted a detailed question on StackOverflow but no one
    has answered yet. (Rails 5 will not render POST made from javascript)

    Basically, I am doing a very simple ajax POST (also tried GET with same
    results), the equivalent of: localhost:3000/lockout/2016-03-16.

    The ajax request gets past routes.rb, and calls the appropriate method in
    the controller. The controller processes it correctly (database is updated,
    debug print statements show up in log, etc.) but it does NOT render.

    When I submit the same request manually from the browser (as a GET) it again
    is processed correctly, but then it DOES render.

    In all cases the log shows this (with times slightly different):

    Started POST "/lockout/2016-03-16" for 127.0.0.1 at 2016-03-06 13:05:22
    -0500
    Processing by LockoutsController#toggle as HTML
    *
    *
    *
    Rendered lockouts/toggle.html.erb within layouts/application (0.6ms)
    Completed 200 OK in 244ms (Views: 203.0ms | ActiveRecord: 11.4ms)

    In fact, the log shows identical entries from the original Started GET/POST
    line for all attempts at invoking the method, but only when the URL is
    entered manually will the view render.

    BTW, the ajax is called from fullcalendar's dayClick event if that helps.
    fullcalendar's eventClick events DO render.

    Does anyone have any ideas?
    Can you show us the code you are you using to do the post?

    Colin
    Hi Colin,

    Sure!

    This is the whole thing (including alerts to let me know when clicks are
    handled):

          <script>
            var AUTH_TOKEN = $('meta[name=csrf-token]').attr('content');
            $(document).ready(function()
            {
              $('#calendar').fullCalendar(
              {
                minTime: "08:00:00",
                maxTime: "20:00:00",
                eventRender: function (event, element) {
                  element.click(function() {});
                },
                eventClick: function(calEvent, jsEvent, view) {
                  alert('Clicked on: ' + calEvent.format());
                },
                dayClick: function(date, jsEvent, view) {
                  alert('You Clicked on: ' + date.format());
                  // $.post('/lockout/'+date.format())
                  $.ajax({
                    url: '/lockout/'+date.format(),
                    type: 'post',
                    dataType: "html",
                    data: "&authenticity_token="+AUTH_TOKEN,
                    success: function(){alert('SUCCESS');},
                    error: function()
                    {
                      alert('error posting date');
                    }
                  });
                },
                events:
                {
                  url: '/populate_events',
                  type: 'GET',
                  error: function()
                  {
                    alert('there was an error while fetching events!');
                  }
                },
                fixedWeekCount: false,
                header:
                {
                  left: 'month,agendaWeek',
                  center: 'title',
                  right: 'today prev,next'
                }
              });
              $('#calendar').fullCalendar('option', 'aspectRatio', 2.2);
            });
          </script>

    It's not very complicated. The alert "You clicked on..." always occurs
    when it is supposed to, and the alert('SUCCESS') occurs immediately
    thereafter.

    The alert('error posting date') only occured when I messed up the
    routing, earlier in my development,

    Thanks,

    Don

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/56DCA4E4.9000308%40ziesig.org.
    For more options, visit https://groups.google.com/d/optout.
  • Colin Law at Mar 6, 2016 at 10:23 pm

    On 6 March 2016 at 21:45, Donald Ziesig wrote:
    ...
    This is the whole thing (including alerts to let me know when clicks are
    handled):

    <script>
    var AUTH_TOKEN = $('meta[name=csrf-token]').attr('content');
    $(document).ready(function()
    {
    $('#calendar').fullCalendar(
    {
    minTime: "08:00:00",
    maxTime: "20:00:00",
    eventRender: function (event, element) {
    element.click(function() {});
    },
    eventClick: function(calEvent, jsEvent, view) {
    alert('Clicked on: ' + calEvent.format());
    },
    dayClick: function(date, jsEvent, view) {
    alert('You Clicked on: ' + date.format());
    // $.post('/lockout/'+date.format())
    $.ajax({
    url: '/lockout/'+date.format(),
    type: 'post',
    dataType: "html",
    data: "&authenticity_token="+AUTH_TOKEN,
    success: function(){alert('SUCCESS');},
    You are not doing anything with the response that comes back on
    success. That should contain the data you are rendering in the
    server.

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLu9wQ2yQ1X2VaC28tjbJGy2KqZ90Tfb6f%3D2ASUPncxxTQ%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • Donald Ziesig at Mar 6, 2016 at 11:59 pm

    On 03/06/2016 05:22 PM, Colin Law wrote:
    On 6 March 2016 at 21:45, Donald Ziesig wrote:
    ...
    This is the whole thing (including alerts to let me know when clicks are
    handled):

    <script>
    var AUTH_TOKEN = $('meta[name=csrf-token]').attr('content');
    $(document).ready(function()
    {
    $('#calendar').fullCalendar(
    {
    minTime: "08:00:00",
    maxTime: "20:00:00",
    eventRender: function (event, element) {
    element.click(function() {});
    },
    eventClick: function(calEvent, jsEvent, view) {
    alert('Clicked on: ' + calEvent.format());
    },
    dayClick: function(date, jsEvent, view) {
    alert('You Clicked on: ' + date.format());
    // $.post('/lockout/'+date.format())
    $.ajax({
    url: '/lockout/'+date.format(),
    type: 'post',
    dataType: "html",
    data: "&authenticity_token="+AUTH_TOKEN,
    success: function(){alert('SUCCESS');},
    You are not doing anything with the response that comes back on
    success. That should contain the data you are rendering in the
    server.

    Colin
    It always pays to have experts look at your code!

    Changing:

    success: function(){alert('SUCCESS');}

    to:

    success: function(){window.location.reload(true);}

    made the difference. It works now.

    Thank you Colin,

    Don



    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/56DCC460.5070102%40ziesig.org.
    For more options, visit https://groups.google.com/d/optout.
  • Tamouse pontiki at Mar 7, 2016 at 12:42 am

    On Sun, Mar 6, 2016 at 5:59 PM, Donald Ziesig wrote:
    On 03/06/2016 05:22 PM, Colin Law wrote:
    On 6 March 2016 at 21:45, Donald Ziesig wrote:

    ...
    This is the whole thing (including alerts to let me know when clicks are
    handled):

    <script>
    var AUTH_TOKEN = $('meta[name=csrf-token]').attr('content');
    $(document).ready(function()
    {
    $('#calendar').fullCalendar(
    {
    minTime: "08:00:00",
    maxTime: "20:00:00",
    eventRender: function (event, element) {
    element.click(function() {});
    },
    eventClick: function(calEvent, jsEvent, view) {
    alert('Clicked on: ' + calEvent.format());
    },
    dayClick: function(date, jsEvent, view) {
    alert('You Clicked on: ' + date.format());
    // $.post('/lockout/'+date.format())
    $.ajax({
    url: '/lockout/'+date.format(),
    type: 'post',
    dataType: "html",
    data: "&authenticity_token="+AUTH_TOKEN,
    success: function(){alert('SUCCESS');},
    You are not doing anything with the response that comes back on
    success. That should contain the data you are rendering in the
    server.

    Colin

    It always pays to have experts look at your code!
    Changing:

    success: function(){alert('SUCCESS');}

    to:

    success: function(){window.location.reload(true);}

    made the difference. It works now.

    Thank you Colin,

    Don

    I'm rather curious, if what you want is a full page reload at that point,
    why you're bothering using AJAX?

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAHUC_t_xK7%2Bjy-NoUigWeV-xj3oE9o%2BeN3Rh-2VQxfc00R_VYA%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • Donald Ziesig at Mar 7, 2016 at 3:33 am

    On 03/06/2016 07:41 PM, tamouse pontiki wrote:
    On Sun, Mar 6, 2016 at 5:59 PM, Donald Ziesig wrote:

    On 03/06/2016 05:22 PM, Colin Law wrote:

    On 6 March 2016 at 21:45, Donald Ziesig wrote:

    ...
    This is the whole thing (including alerts to let me know
    when clicks are
    handled):

    <script>
    var AUTH_TOKEN =
    $('meta[name=csrf-token]').attr('content');
    $(document).ready(function()
    {
    $('#calendar').fullCalendar(
    {
    minTime: "08:00:00",
    maxTime: "20:00:00",
    eventRender: function (event, element) {
    element.click(function() {});
    },
    eventClick: function(calEvent, jsEvent, view) {
    alert('Clicked on: ' + calEvent.format());
    },
    dayClick: function(date, jsEvent, view) {
    alert('You Clicked on: ' + date.format());
    // $.post('/lockout/'+date.format())
    $.ajax({
    url: '/lockout/'+date.format(),
    type: 'post',
    dataType: "html",
    data: "&authenticity_token="+AUTH_TOKEN,
    success: function(){alert('SUCCESS');},

    You are not doing anything with the response that comes back on
    success. That should contain the data you are rendering in the
    server.

    Colin

    It always pays to have experts look at your code!

    Changing:

    success: function(){alert('SUCCESS');}

    to:

    success: function(){window.location.reload(true);}

    made the difference. It works now.

    Thank you Colin,

    Don


    I'm rather curious, if what you want is a full page reload at that
    point, why you're bothering using AJAX?
    Because I don't know any other way to go from the fullcalendar
    javascript event to the server. :'( . If you have a better approach,
    please tell me how. It is never too late to learn something new (I'm 73
    years old and started programming in the late 1950's. I've been
    learning a lot of new things since then ;-) ).

    I would really like to process the dayclick event on the server, prompt
    the user for a response, then reload the page. That implies that I need
    to render html or json to do the prompting, then reload the page with
    data conditioned on the user's response. So far I haven't been able to
    do that.

    Thanks

    Don
    --
    You received this message because you are subscribed to the Google
    Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to rubyonrails-talk+unsubscribe@googlegroups.com
    To post to this group, send email to rubyonrails-talk@googlegroups.com
    To view this discussion on the web visit
    https://groups.google.com/d/msgid/rubyonrails-talk/CAHUC_t_xK7%2Bjy-NoUigWeV-xj3oE9o%2BeN3Rh-2VQxfc00R_VYA%40mail.gmail.com
    <https://groups.google.com/d/msgid/rubyonrails-talk/CAHUC_t_xK7%2Bjy-NoUigWeV-xj3oE9o%2BeN3Rh-2VQxfc00R_VYA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/56DCF681.2050106%40ziesig.org.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMar 6, '16 at 6:22p
activeMar 7, '16 at 3:33a
posts7
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase