Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在验证之前显示视图_Ruby On Rails_Ruby On Rails 4_Omniauth - Fatal编程技术网

Ruby on rails 在验证之前显示视图

Ruby on rails 在验证之前显示视图,ruby-on-rails,ruby-on-rails-4,omniauth,Ruby On Rails,Ruby On Rails 4,Omniauth,我想为社交网络创建一个登录名,例如Twitter、Facebook等 但也有像twitter这样的社交网络不给我发电子邮件 在保存用户以显示一个表单视图,在该视图中添加电子邮件,然后保存Twitter+电子邮件的所有数据之前,我如何执行此操作?我假设您知道如何设置omniauth,因此我将直接跳到相关部分 在回调中,如果找到指定uid和提供程序的用户,则将其登录并重定向。否则,您需要在会话中存储omniauth有效负载,并重定向到用户可以添加电子邮件的表单: def twitter if U

我想为社交网络创建一个登录名,例如Twitter、Facebook等

但也有像twitter这样的社交网络不给我发电子邮件


在保存用户以显示一个表单视图,在该视图中添加电子邮件,然后保存Twitter+电子邮件的所有数据之前,我如何执行此操作?

我假设您知道如何设置omniauth,因此我将直接跳到相关部分

在回调中,如果找到指定uid和提供程序的用户,则将其登录并重定向。否则,您需要在会话中存储omniauth有效负载,并重定向到用户可以添加电子邮件的表单:

def twitter
  if User.find_by(provider: auth.provider, uid: auth.uid)
    # sign in user and redirect
  else
    session[:omniauth] ||= {}
    session[:omniauth][auth.uid] = auth
    redirect_to new_omniauth_email_request_path(ominauth_uid: auth.uid)
  end
end

private

def auth
  request.env['omniauth.auth']
end
将其添加到routes.rb时:

resources :omniauth, only: [], param: :uid do
  resource :email_request, only: [:new, :create]
end
您将拥有以下两条新路线:

omniauth_email_request     POST  /omniauth/:omniauth_uid/email_request(.:format)       email_requests#create
new_omniauth_email_request GET   /omniauth/:omniauth_uid/email_request/new(.:format)   email_requests#new
您需要创建
EmailRequestsController
并实施
new
create
操作。成功创建用户后,不要忘记清除会话:

def create
  # ...
  @user = User.create do |user|
    user.email = params['email']
    user.uid = params['omniauth_uid']
    user.name = session[:omniauth][user.uid].name
    # assign whatever else you may need from session[:omniauth][user.uid]
  end

  session[:omniauth].delete @user.uid if @user.persisted?
  # ...
end
请注意,如果您计划支持多个omniauth提供商,则在“中间步骤”中,用户可能会提供数据库中已经存在的电子邮件(例如,用户已经通过传递电子邮件的其他omniauth进行了身份验证)。这个案子需要处理