开发者

Why is rails trying to redirect?

I am working on a new app and am trying to customize where Devise sends the signed in or newly registered User after the submission of the sign-in form.

I want to send the User back to the page where the sign-in action was called from so I have created these methods in application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery

  def store_location
    session[:origin_url] = request.request_uri if request.get? and controller_name != "user_sessions" and controller_name != "sessions"
  end

  def redirect_back_or_default(default)
    redirect_to(session[:origin_url] || default)
  end


  def after_sign_in_path_for(resource_or_scope)
  [#https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in][1]
    p "*********** About to check session"
    if session[:origin_url]
      p "*****************Redirecting after sign_in to " + session[:origin_url]
      redirect_back_or_default(docs_path)
      return #this syntax needs to be checked
    else
     super
    end
    return
  end
end

Here is the docs_controller, show action code:

  # GET /docs/1
  # GET /docs/1.xml
  def show
    @doc = Doc.find(params[:id])
    store_location

    respond_to do |format|
      format.html #开发者_如何转开发 show.html.erb
      format.xml  { render :xml => @doc }
    end
  end

And here is the output from the server log:

"*********** About to check session"
"*****************Redirecting after sign_in to /docs/3"


Started GET "/users/sign_in" for 127.0.0.1 at Wed Mar 02 01:12:16 +0100 2011
  Processing by Devise::SessionsController#new as HTML
  User Load (0.9ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1
Redirected to http://localhost:3000/docs/3
Redirected to 
Completed   in 119ms

ActionController::ActionControllerError (Cannot redirect to nil!):


Rendered /home/jon/.rvm/gems/ruby-1.8.7-p334@rails3/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.7ms)
Rendered /home/jon/.rvm/gems/ruby-1.8.7-p334@rails3/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (955.7ms)
Rendered /home/jon/.rvm/gems/ruby-1.8.7-p334@rails3/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (986.3ms)

I'm using Rails 3.0.5

Thanks for any advice...


after_sign_in_path_for (like the name says) should return a path and not do any redirect_to magic.

Since you just return all over the place without any return value, Devise might get a nil from you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜