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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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 向Omniauth用户模型添加字段_Ruby On Rails_Ruby On Rails 4_Omniauth - Fatal编程技术网

Ruby on rails 向Omniauth用户模型添加字段

Ruby on rails 向Omniauth用户模型添加字段,ruby-on-rails,ruby-on-rails-4,omniauth,Ruby On Rails,Ruby On Rails 4,Omniauth,我在RoR 4 web应用程序中使用omniauth和身份策略 遵循Ryan的RailsCast,并以以下方式生成用户模型: $ rails g model user provider:string uid:string name:string 在我的用户模型中,我有: class User < ActiveRecord::Base has_many :profiles has_many :packages has_many :transactions has_many

我在RoR 4 web应用程序中使用omniauth和身份策略

遵循Ryan的RailsCast,并以以下方式生成用户模型:

$ rails g model user provider:string uid:string name:string
在我的用户模型中,我有:

class User < ActiveRecord::Base
  has_many :profiles
  has_many :packages
  has_many :transactions
  has_many :links
  def self.from_omniauth(auth)
    find_by_provider_and_uid(auth["provider"], auth["uid"]) || create_with_omniauth(auth)
  end

  def self.create_with_omniauth(auth)
    create! do |user|
      user.provider = auth["provider"]
      user.uid = auth["uid"]
      user.name = auth["info"]["name"]
    end
  end
end
2-将自定义字段添加到我的表单中:

<%= hidden_field_tag :stripe_customer_id %>
4-我的问题是:如何在我的用户模型中获得这个值

class User < ActiveRecord::Base
  has_many :profiles
  has_many :packages
  has_many :transactions
  has_many :links
  def self.from_omniauth(auth)
    find_by_provider_and_uid(auth["provider"], auth["uid"]) || create_with_omniauth(auth)
  end

  def self.create_with_omniauth(auth)
    create! do |user|
      user.provider = auth["provider"]
      user.uid = auth["uid"]
      user.name = auth["info"]["name"]
      user.stripe_customer_id = ?????????????
    end
  end
end
class用户

谢谢:)

您只能获取Omniauth提供商提供的数据。以下是已引用的列表:

以下是您在设计上添加许可证的方式:

在应用程序中_controller.rb

 before_action :configure_permitted_parameters, if: :devise_controller?


protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:fullname, :email, :password, :password_confirmation) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:avatar, :fullname, :email, :password, :password_confirmation, :current_password) }
  end
您还可以通过以下操作创建自己的控制器:

# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
  def new
    super
  end

  def create
    # add custom create logic here
  end

  def update
    super
  end
end 

以下是一个可能的解决方案:

会话控制器调用用户控制器中的metod self.from_omniauth(auth)。在会话控制器中,我拥有所需的所有参数

修改的用户模型:

class User < ActiveRecord::Base
  has_many :profiles
  has_many :packages
  has_many :transactions
  has_many :links
  def self.from_omniauth(auth,stripe_customer_id)
    find_by_provider_and_uid(auth["provider"], auth["uid"]) || create_with_omniauth(auth,stripe_customer_id)
  end

  def self.create_with_omniauth(auth,stripe_customer_id)
    create! do |user|
      user.provider = auth["provider"]
      user.uid = auth["uid"]
      user.name = auth["info"]["name"]
      user.stripe_customer_id = stripe_customer_id
    end
  end
end

这对我没什么帮助。无论如何,谢谢你抽出时间。
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
  def new
    super
  end

  def create
    # add custom create logic here
  end

  def update
    super
  end
end 
# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
class User < ActiveRecord::Base
  has_many :profiles
  has_many :packages
  has_many :transactions
  has_many :links
  def self.from_omniauth(auth,stripe_customer_id)
    find_by_provider_and_uid(auth["provider"], auth["uid"]) || create_with_omniauth(auth,stripe_customer_id)
  end

  def self.create_with_omniauth(auth,stripe_customer_id)
    create! do |user|
      user.provider = auth["provider"]
      user.uid = auth["uid"]
      user.name = auth["info"]["name"]
      user.stripe_customer_id = stripe_customer_id
    end
  end
end
  def create
    user = User.from_omniauth(env["omniauth.auth"], params["stripe_customer_id"])
    session[:user_id] = user.id
    redirect_to profiles_url
  end