2

My user experience involves users submitting a form before they've authenticated (using omniauth). I started doing something like this:

    def self.require_facebook_authentication!(options={})
      before_filter :redirect_to_facebook_if_not_authenticated options
    end

    def redirect_to_facebook_if_not_authenticated
      if !logged_in?
        session[:param_cache] = params
        session[:original_destination] = request.fullpath
        redirect_to '/auth/facebook'  

      end
    end

Then, on hitting the auth callback, redirect to a page that submits a form with the post params inline, for a total of 3 redirects (/stuff/new/ on POST -> auth/facebook -> facebook -> /auth/facebook/callback [ html template with POST form ] -> /stuff/create). I'd rather not create an authentication popup; instead, I'd like to navigate to a separate page, log in, and redirect to the completed action.

I'm fairly new to Rails, so I'm still learning - is this already built in to another framework? Am I missing something really basic? Thanks in advance!

Anthony Bishopric
  • 1,306
  • 11
  • 23

2 Answers2

4

if you are asking as to whether or not there is a "RAILS" way that will automatically post the data after a redirect, the answer is no (see https://stackoverflow.com/questions/985596/redirect-to-using-post-in-rails)

In my opinion the safest, easiest, and most RESTful way to achieve what you want would be to simply have the params you are eventually posting stored in session so that you can redirect back to the original 'new' page and have the form automatically prefilled with the post data. Sure this is one extra step for the user, but since REST doesn't allow for redirects to POSTs it is imo the cleanest way to go about it

Community
  • 1
  • 1
Will Ayd
  • 6,767
  • 2
  • 36
  • 39
0

There may be a better way, but if you render this after authenticating, then the client will Ajax post the form contents then redirect.

<script>
  new Ajax.Request(<%= session[:original_destination] %>, {
    method: 'post',
    params: '<%= session[:param_cache].to_query %>',
    onSuccess: function(){
      window.location = '<%= session[:original_destination] %>';
    }
  });
</script>
Unixmonkey
  • 18,485
  • 7
  • 55
  • 78
  • Won't this try to go to the original_destination twice? (Once with the POST data and then again without?) – Anthony Bishopric Mar 18 '11 at 04:28
  • @Anthony Bishopric: True. You'd want to capture and change the window.location call to be where the user would be sent after a successful post. – Unixmonkey Mar 18 '11 at 13:23