views:

230

answers:

2

During a post for a new model, I am checking for authentication via Authlogic. There is a before_filter on the create request. It is calling require_user. After the user session has been successfully created, the redirect_back_or_default(default) method is called. The problem is, The request needs to be posted to the stored uri. I have tried to store the method and input it into the redirect_to however it isn't working. Any ideas?

# called before successful authentication with before_filter
def require_user
  unless current_user
    store_location
    flash[:notice] = "You must be logged in to access this page"
    redirect_to new_user_session_url
    return false
  end
end

def store_location
  session[:return_to] = request.request_uri
  session[:return_to_method] = request.request_method
end

# called after successful authentication
def redirect_back_or_default(default)
  redirect_to((session[:return_to] ? session[:return_to] : default), :method => session[:return_to_method])
  session[:return_to] = nil
  session[:return_to_method] = nil
end
A: 

You can't redirect to a post action, only get actions.

JimNeath
A: 

You could store the post object for later processing after authentication, but you really don't want to do that.

Why not simply ask for authentication on the #new method, rather than (or in addition to) the #create? That way the user is authenticated before they fill in the form.

askegg
Thanks for the answers. What I ended up doing is adding a state of authorized to the model. I allow creates from unauthorized users that are not signed in and the initial state is invalid. Then in the create action, if they aren't signed in it will store the path to the show action and redirect to the login/new user page. If they don't login the model will never get validated and will be reaped in a rake task at a later date. If they do login they will be redirected back to the show action which will 'authorize' the model if it was created by them and they are logged_in.
Blake Chambers