Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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 如何在通过facebook rails 4登录后为用户表创建额外列_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 如何在通过facebook rails 4登录后为用户表创建额外列

Ruby on rails 如何在通过facebook rails 4登录后为用户表创建额外列,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,如果当前用户是新用户,我需要在通过facebook登录后添加类别。(类别包含项目列表) 用户模型 class User < ActiveRecord::Base attr_accessible :email, :name, :uid, :provider validates_presence_of :name, :uid validates_uniqueness_of :uid has_many :votes def self.from_omniauth(auth) where(

如果当前用户是新用户,我需要在通过facebook登录后添加类别。(类别包含项目列表)

用户模型

class User < ActiveRecord::Base
attr_accessible :email, :name, :uid, :provider
validates_presence_of :name, :uid
validates_uniqueness_of :uid

has_many :votes 

def self.from_omniauth(auth)
  where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|

    user.provider = auth.provider
    user.uid = auth.uid
    user.name = auth.info.name          
    user.save!
  end
end
class用户
用户控制器

class UsersController < ApplicationController
respond_to :html, :json

def create   
  @user = User.from_omniauth(env["omniauth.auth"])    
  session[:user_id] = @user.id
  redirect_to browsing_url
 end
end
class UsersController
应用程序控制器

class ApplicationController < ActionController::Base

protect_from_forgery with: :null_session   

private  

def current_user
@current_user ||= User.find(session[:user_id])if session[:user_id]
end

helper_method :current_user
end
class ApplicationController

如果当前用户是现有用户,则应重定向到浏览url。它工作正常,但我的问题是,如果当前用户是新用户,如何创建类别。

如果您想在登录后根据登录用户是否为新用户的条件重定向用户。您可以在
用户
模型中拥有一个
属性访问器
,并基于此,您可以在控制器中重定向。就像下面的代码

在您的
用户
型号中-

class User < ActiveRecord::Base
  attr_accessor :new_user   

end
在用户控制器中

class UsersController < ApplicationController
  respond_to :html, :json

  def create   
    @user = User.from_omniauth(env["omniauth.auth"])    
    session[:user_id] = @user.id
    if @user.new_user
      redirect_to 'other_path'
    else
      redirect_to browsing_url
    end
  end
end
class UsersController
一般来说,根据用户操作更改实际模式是个坏主意。您应该重构应用程序,以便在现有表中创建记录,而不是创建整个新列。是否确实要向表中添加类别列?或者你只是想创建一个类别记录,或者设置一个用户的
Category\u id
,而不是更改表格模式?@MaxWilliams如果当前用户是新用户,我需要创建一个类别记录,然后你能编辑你的问题来更改标题,并改进措辞吗?这实际上是海报想要做的吗?我认为标题可能有误导性,他们实际上只是想创建一个类别记录。对不起,我不是故意的,事实上,如果当前用户是新用户,我必须创建一个包含项目列表的类别。然后它应该是一个单独的表,就像@MaxWilliams所说的那样。您可以使用
name
字段生成一个
Category
模型。@VishnuAtrai是的,您是对的,我有一个Category模型。但我的问题是,当用户通过facebook登录时,
from_omniauth
从facebook获取用户的详细信息,之后我需要检查当前用户是否是新用户,如果是,他必须在表中插入一个类别,否则他应该重定向到浏览url。对不起我的英语,你明白我的疑问是什么吗?@saravana我在代码中添加了条件,如果用户需要,你可以添加一个条件
if user.new\u record?
class UsersController < ApplicationController
  respond_to :html, :json

  def create   
    @user = User.from_omniauth(env["omniauth.auth"])    
    session[:user_id] = @user.id
    if @user.new_user
      redirect_to 'other_path'
    else
      redirect_to browsing_url
    end
  end
end