How to execute Angular $http.post to rails controller followed by redirect?












0















I would like a method on a button to execute a post redirect, and then have the page redirected by rails according to the controller's action.



Now I have the following method attached to a button on an angular page running in the page's Angular controller:



 $scope.addClass = function(class_id_string) {
let payload = {class_id: class_id_string};
$http({
method: 'POST',
url: ADD_CLASS_ACCOUNT_URL,
data: payload
});
}


In my Rails controller, I have the following code:



 def save_class_account
class_id = params[:class_id]
return redirect_to display_chart_path unless class_id.nil?
[...]
end


I have confirmed that the controller method is being called, and that the class_id parameter is coming through in the params collection. But still, nothing happens on the page when this method is called. I assume what is actually happening is that the entire page is being returned to the $http post method and ignored, instead of redirecting the browser, but I am not sure what is really going on here. But the page doesn't redirect as desired.



How can I get the redirect to happen?










share|improve this question




















  • 1





    Your assumption about the whole page returned is correct. You could submit a regular form instead of using ajax ($http) or return data to tell javascript where to redirect to

    – charlietfl
    Nov 12 '18 at 20:49


















0















I would like a method on a button to execute a post redirect, and then have the page redirected by rails according to the controller's action.



Now I have the following method attached to a button on an angular page running in the page's Angular controller:



 $scope.addClass = function(class_id_string) {
let payload = {class_id: class_id_string};
$http({
method: 'POST',
url: ADD_CLASS_ACCOUNT_URL,
data: payload
});
}


In my Rails controller, I have the following code:



 def save_class_account
class_id = params[:class_id]
return redirect_to display_chart_path unless class_id.nil?
[...]
end


I have confirmed that the controller method is being called, and that the class_id parameter is coming through in the params collection. But still, nothing happens on the page when this method is called. I assume what is actually happening is that the entire page is being returned to the $http post method and ignored, instead of redirecting the browser, but I am not sure what is really going on here. But the page doesn't redirect as desired.



How can I get the redirect to happen?










share|improve this question




















  • 1





    Your assumption about the whole page returned is correct. You could submit a regular form instead of using ajax ($http) or return data to tell javascript where to redirect to

    – charlietfl
    Nov 12 '18 at 20:49
















0












0








0








I would like a method on a button to execute a post redirect, and then have the page redirected by rails according to the controller's action.



Now I have the following method attached to a button on an angular page running in the page's Angular controller:



 $scope.addClass = function(class_id_string) {
let payload = {class_id: class_id_string};
$http({
method: 'POST',
url: ADD_CLASS_ACCOUNT_URL,
data: payload
});
}


In my Rails controller, I have the following code:



 def save_class_account
class_id = params[:class_id]
return redirect_to display_chart_path unless class_id.nil?
[...]
end


I have confirmed that the controller method is being called, and that the class_id parameter is coming through in the params collection. But still, nothing happens on the page when this method is called. I assume what is actually happening is that the entire page is being returned to the $http post method and ignored, instead of redirecting the browser, but I am not sure what is really going on here. But the page doesn't redirect as desired.



How can I get the redirect to happen?










share|improve this question
















I would like a method on a button to execute a post redirect, and then have the page redirected by rails according to the controller's action.



Now I have the following method attached to a button on an angular page running in the page's Angular controller:



 $scope.addClass = function(class_id_string) {
let payload = {class_id: class_id_string};
$http({
method: 'POST',
url: ADD_CLASS_ACCOUNT_URL,
data: payload
});
}


In my Rails controller, I have the following code:



 def save_class_account
class_id = params[:class_id]
return redirect_to display_chart_path unless class_id.nil?
[...]
end


I have confirmed that the controller method is being called, and that the class_id parameter is coming through in the params collection. But still, nothing happens on the page when this method is called. I assume what is actually happening is that the entire page is being returned to the $http post method and ignored, instead of redirecting the browser, but I am not sure what is really going on here. But the page doesn't redirect as desired.



How can I get the redirect to happen?







ruby-on-rails angularjs ruby-on-rails-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 21:16









georgeawg

33k104968




33k104968










asked Nov 12 '18 at 20:42









user1023110user1023110

1,0301723




1,0301723








  • 1





    Your assumption about the whole page returned is correct. You could submit a regular form instead of using ajax ($http) or return data to tell javascript where to redirect to

    – charlietfl
    Nov 12 '18 at 20:49
















  • 1





    Your assumption about the whole page returned is correct. You could submit a regular form instead of using ajax ($http) or return data to tell javascript where to redirect to

    – charlietfl
    Nov 12 '18 at 20:49










1




1





Your assumption about the whole page returned is correct. You could submit a regular form instead of using ajax ($http) or return data to tell javascript where to redirect to

– charlietfl
Nov 12 '18 at 20:49







Your assumption about the whole page returned is correct. You could submit a regular form instead of using ajax ($http) or return data to tell javascript where to redirect to

– charlietfl
Nov 12 '18 at 20:49














1 Answer
1






active

oldest

votes


















1














Angular is using Ajax calls using $http service. It means update a web page/Send data to server without reloading the page.



When you post data using HTTP Service, it will call Rails Controller methods and do the action and send the response back to Ajax.



Here page redirection doesn't work from Rails Controller. If you want to redirect page, Please do as shown below in the $http service itself.



$scope.addClass = function(class_id_string) {
let payload = {class_id: class_id_string};
$http({
method: 'POST',
url: ADD_CLASS_ACCOUNT_URL,
data: payload
}).then(function mySuccess(response) {
window.location = "Paste Redirection URL here"
}, function myError(response) {
"Show Error message here"
});
}


And also in the Rails Controller, When you call methods from Ajax, It will send JSON response as shown below



def save_class_account
class_id = params[:class_id]
render json: {url: display_chart_path, mgs: 'Successfully created'}, status: :created}
end





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269792%2fhow-to-execute-angular-http-post-to-rails-controller-followed-by-redirect%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Angular is using Ajax calls using $http service. It means update a web page/Send data to server without reloading the page.



    When you post data using HTTP Service, it will call Rails Controller methods and do the action and send the response back to Ajax.



    Here page redirection doesn't work from Rails Controller. If you want to redirect page, Please do as shown below in the $http service itself.



    $scope.addClass = function(class_id_string) {
    let payload = {class_id: class_id_string};
    $http({
    method: 'POST',
    url: ADD_CLASS_ACCOUNT_URL,
    data: payload
    }).then(function mySuccess(response) {
    window.location = "Paste Redirection URL here"
    }, function myError(response) {
    "Show Error message here"
    });
    }


    And also in the Rails Controller, When you call methods from Ajax, It will send JSON response as shown below



    def save_class_account
    class_id = params[:class_id]
    render json: {url: display_chart_path, mgs: 'Successfully created'}, status: :created}
    end





    share|improve this answer




























      1














      Angular is using Ajax calls using $http service. It means update a web page/Send data to server without reloading the page.



      When you post data using HTTP Service, it will call Rails Controller methods and do the action and send the response back to Ajax.



      Here page redirection doesn't work from Rails Controller. If you want to redirect page, Please do as shown below in the $http service itself.



      $scope.addClass = function(class_id_string) {
      let payload = {class_id: class_id_string};
      $http({
      method: 'POST',
      url: ADD_CLASS_ACCOUNT_URL,
      data: payload
      }).then(function mySuccess(response) {
      window.location = "Paste Redirection URL here"
      }, function myError(response) {
      "Show Error message here"
      });
      }


      And also in the Rails Controller, When you call methods from Ajax, It will send JSON response as shown below



      def save_class_account
      class_id = params[:class_id]
      render json: {url: display_chart_path, mgs: 'Successfully created'}, status: :created}
      end





      share|improve this answer


























        1












        1








        1







        Angular is using Ajax calls using $http service. It means update a web page/Send data to server without reloading the page.



        When you post data using HTTP Service, it will call Rails Controller methods and do the action and send the response back to Ajax.



        Here page redirection doesn't work from Rails Controller. If you want to redirect page, Please do as shown below in the $http service itself.



        $scope.addClass = function(class_id_string) {
        let payload = {class_id: class_id_string};
        $http({
        method: 'POST',
        url: ADD_CLASS_ACCOUNT_URL,
        data: payload
        }).then(function mySuccess(response) {
        window.location = "Paste Redirection URL here"
        }, function myError(response) {
        "Show Error message here"
        });
        }


        And also in the Rails Controller, When you call methods from Ajax, It will send JSON response as shown below



        def save_class_account
        class_id = params[:class_id]
        render json: {url: display_chart_path, mgs: 'Successfully created'}, status: :created}
        end





        share|improve this answer













        Angular is using Ajax calls using $http service. It means update a web page/Send data to server without reloading the page.



        When you post data using HTTP Service, it will call Rails Controller methods and do the action and send the response back to Ajax.



        Here page redirection doesn't work from Rails Controller. If you want to redirect page, Please do as shown below in the $http service itself.



        $scope.addClass = function(class_id_string) {
        let payload = {class_id: class_id_string};
        $http({
        method: 'POST',
        url: ADD_CLASS_ACCOUNT_URL,
        data: payload
        }).then(function mySuccess(response) {
        window.location = "Paste Redirection URL here"
        }, function myError(response) {
        "Show Error message here"
        });
        }


        And also in the Rails Controller, When you call methods from Ajax, It will send JSON response as shown below



        def save_class_account
        class_id = params[:class_id]
        render json: {url: display_chart_path, mgs: 'Successfully created'}, status: :created}
        end






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 9:42









        visnuvisnu

        876616




        876616






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269792%2fhow-to-execute-angular-http-post-to-rails-controller-followed-by-redirect%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation