6 Replies Latest reply on Jul 30, 2013 4:08 AM by Val Palhories

    Redirect issues

    Val Palhories

      Hello,

       

      I'm having a hell of a time trying to get my controller to redirect to another controller.

       

      The code inside my view is as follows:

       

      $('#btn_save').click(function(){

              $.post("<%= url_for :controller => :Configuration, :action => :save_syncurl %>",

                 {

                   syncurl:syncurl

                  });

            });

       

      At the end of the save_syncurl action inside the Configuration controller, I have the following statements:

       

           File.open(fileName,'w') do |f|

            f.write(@json_array.to_json)

          end

       

      followed immediately by

         

          redirect :controller=>:Login,:action => :login

       

      My file gets updated so I know it works up until there. After that, nothing, no errors, nothing!

       

      Here is the last line in my log file:

       

      I 07/18/2013 17:40:01:576 00001620           HttpServer| Process URI: '/app/Configuration/save_syncurl'

      I 07/18/2013 17:40:01:576 00001620                  APP| RHO serve: /app/Configuration/save_syncurl

      I 07/18/2013 17:40:01:576 00001620              RhoRuby| require_compiled: Configuration/configuration_controller

      I 07/18/2013 17:40:01:607 00001620              RhoRuby| require_compiled: helpers/browser_helper

      I 07/18/2013 17:40:01:607 00001620              RhoRuby| require_compiled: helpers/navigation_helper

      I 07/18/2013 17:40:01:607 00001620              RhoRuby| require_compiled: helpers/db_helper

      I 07/18/2013 17:40:01:623 00001620              RhoRuby| require_compiled: helpers/config_helper

      I 07/18/2013 17:40:01:623 00001620              RhoRuby| require_compiled: helpers/application_helper

      I 07/18/2013 17:40:01:638 00001620              RhoRuby| require_compiled: helpers/config_helper

      I 07/18/2013 17:40:01:638 00001620              RhoRuby| require_compiled: helpers/model_helper

      I 07/18/2013 17:40:01:654 00001620                  APP| Params: {"syncurl"=>"http://localhost/scout/scoutsync/api/"}

      I 07/18/2013 17:40:01:701 00001620           HttpServer| GC Start.

      I 07/18/2013 17:40:01:716 00001620           HttpServer| GC End.

       

      I've also tried this:

       

      WebView.navigate url_for(:controller=>:Login,:action => :login)

       

      Am I doing something wrong?

       

      Thanks.

       

      --- Val

        • Re: Redirect issues
          Val Palhories

          Also tried this:

           

          $('#btn_save').click(function(){

                  $.post("<%= url_for :controller => :Configuration, :action => :save_syncurl %>",

                     {

                       syncurl:syncurl

                      });

                  <% ApplicationHelper.TakeToControllerAction(redirect :controller => :Login, :action => :login) %>

                });

           

           

          The TakeToControllerActionMethod is as follows inside the ApplicationHelper

           

          def self.TakeToControllerAction(url)   

              #WebView.navigate(url)

              WebView.execute_js("SetWindowLocation('#{url}');")

            end   

           

          But it executes the action immediately when the page loads.

           

          --- Val

           

          Message was edited by: Val Palhories. Forgot to add the code for the TakeToControllerAction method.

            • Re: Redirect issues
              Kutir Mobility

              Hi Val,

              I couldn't able to debug much with the code samples.

              Can you attach your app source code as zip ?

               

              Surendran S

              Kutir Mobility

                • Re: Redirect issues
                  Val Palhories

                  Serendran

                   

                  I found a work-around by doing the following:

                   

                  <a id="btnReturn" href="<%= url_for(:controller => :Login, :action => :login) %>" data-transition="fade"></a>

                   

                  $('#btn_save').click(function(){

                          BlockBody();

                          $.post("<%= url_for :controller => :Configuration, :action => :save_syncurl %>",

                             {

                               syncurl:syncurl

                              });

                          UnblockBody();

                          $('#btnReturn').click();

                        });

                   

                  I'm still perplexed as to why what I had in the first place didn't work.

                   

                  On a similar note, maybe you can answer this question for me:

                   

                  I have the following code in my view:

                   

                  <%if (Date.today < Date.parse(Settings.GetDateForComparison.to_s))%>

                              $('#alert').notifyError("The date on your device is incorrect. Please set the date using your device's settings and restart Scout. Scout will now exit");

                              setTimeout("timeout_function()", 5000)

                              BlockBody();

                          <%end%>

                     

                          function timeout_function(){

                                         UnblockBody();

                               <% System.exit %>

                          }

                   

                  When the view is rendered it immediately executes the System.exit command. Any idea why? Shouldn't it only execute if I call the method from JavaScript like I did in the block above it?

                   

                  Thanks

                   

                  -- Val

                    • Re: Redirect issues
                      Kutir Mobility

                      Hi Val,

                       

                      The render call in a controller causes the corresponding .erb to be evaluated and returned to the WebView (browser). Ruby code inside <% %> tags in an .erb file is executed as it is found and this happens at response-building time, before the HTML is returned for display. A call to exit will abort everything and the response will never be sent, which is the behavior you are experiencing.

                       

                      If you want to call System.exit after a timeout, create a new action in your controller and use an AJAX call in your timeout_function() to call this new action:

                       

                      def exit_application

                           System.exit

                      end

                       

                      function timeout_function() {

                           $.ajax("/app/YourController/exit_application");

                      }

                       

                      Hope that helps,

                      Javier

                      Kutir Mobility