Skip to content

Instantly share code, notes, and snippets.

@yao2030
Created August 17, 2012 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yao2030/3379045 to your computer and use it in GitHub Desktop.
Save yao2030/3379045 to your computer and use it in GitHub Desktop.
make current_user available in authorizations.rb
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
class AuthorizationsController < ApplicationController
def index
@authorizations = Authorization.all
end
def create
auth = request.env['omniauth.auth']
current_user.authorizations.find_or_create_by_provider(auth['provider'], auth['uid'])
redirect_to root_url, :notice => "Authorizations successfull"
end
def destroy
@authorization = current_user.authorizations.find(params[:id])
@authorization.destroy
redirect_to root_url, :notice => "Successfully destroyed authorizations"
end
def failure
end
end
M", "provider"=>"twitter"}
2012-08-17T14:18:33+00:00 app[web.1]: Completed 500 Internal Server Error in 0ms
2012-08-17T14:18:33+00:00 app[web.1]:
2012-08-17T14:18:33+00:00 app[web.1]: NoMethodError (undefined method `authorizations' for nil:NilClass):
2012-08-17T14:18:33+00:00 app[web.1]:
2012-08-17T14:18:33+00:00 app[web.1]: app/controllers/authorizations_controller.rb:9:in `create'
@mimosz
Copy link

mimosz commented Aug 17, 2012

当前用户是nil, 逻辑错啦.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment