Ruby on rails Rails 4.0设计-多态-注册后重定向到用户

Ruby on rails Rails 4.0设计-多态-注册后重定向到用户,ruby-on-rails,authentication,ruby-on-rails-4,devise,polymorphic-associations,Ruby On Rails,Authentication,Ruby On Rails 4,Devise,Polymorphic Associations,在过去的几个小时里,我在谷歌上搜索了一个解决方案,我觉得这个问题值得发布——对rails来说是个新问题,但我愿意学习 我已经使用Desive在rails 4上实现了用户身份验证。有两种不同类型的用户可以注册到该站点。每种类型都需要存储不同的信息(即:类型A需要电话号码,而类型B不需要) 长话短说——我喜欢多态关联。所以我现在有了普通的用户表和另外两个表,它们存储了每种类型的附加信息。为了实现这一点,我遵循以下步骤: 一切都很顺利 但是,当类型a的用户注册到站点时(使用用户a的控制器/视图和模型

在过去的几个小时里,我在谷歌上搜索了一个解决方案,我觉得这个问题值得发布——对rails来说是个新问题,但我愿意学习

我已经使用Desive在rails 4上实现了用户身份验证。有两种不同类型的用户可以注册到该站点。每种类型都需要存储不同的信息(即:类型A需要电话号码,而类型B不需要)

长话短说——我喜欢多态关联。所以我现在有了普通的用户表和另外两个表,它们存储了每种类型的附加信息。为了实现这一点,我遵循以下步骤:

一切都很顺利

但是,当类型a的用户注册到站点时(使用用户a的控制器/视图和模型),成功后用户不会登录。如果用户A在注册后自动登录,我必须做什么

这里是我迄今为止的代码(我有一个用户表和一个厨师表)

控制器:

用户\u controller.rb

class UsersController
chefs_controller.rb

class ChefsController
型号:

user.rb

class用户auth.provider,:uid=>auth.uid.to_s,:token=>auth.credentials.token,:secret=>auth.credentials.secret)。首先初始化
如果authorization.user.blank?
用户=当前用户。无?User.where('email=?',auth[“info”][“email”])。第一个:当前用户
如果user.blank?
user=user.new
user.password=design.friendly_令牌[0,10]
user.name=auth.info.name
user.email=auth.info.email
auth.provider==“twitter”?user.save(:validate=>false):user.save
结束
authorization.username=auth.info.昵称
authorization.user\u id=user.id
授权保存
结束
授权用户
结束
私有的
def set_角色
case-meta_型
当“厨师”
添加角色:厨师
其他的
添加角色:Baer
结束
结束
结束
大厨

class-Chef
感谢您的帮助!谢谢你

很好的帖子-

使用此功能的标准方法是使用以下方法覆盖Desive Sessions Controller:

就我个人而言,我不会使用Deave的多态关联。我会创建一个单独的帐户,并使用一个帐户来授权他们处理不同的事情(有点像admin/mo)
class UsersController < ApplicationController
  before_filter :set_user, only: [:show, :edit, :update]
  before_filter :validate_authorization_for_user, only: [:edit, :update]


  # GET /users/1
  def show
  end

  # GET /users/1/edit
  def edit
  end


  # PATCH/PUT /users/1
  def update
    if @user.update_attributes(user_params)
      #debugger
      redirect_to @user, notice: 'User was successfully updated.'
    else
      render action: 'edit'
    end
  end


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    def validate_authorization_for_user
       redirect_to root_path unless @user == current_user
    end

    private

    def user_params
      params.require(:user).permit(:name, :about)
    end
  end
class ChefsController < ApplicationController

  before_action :set_chef, only: [:show, :edit, :update, :destroy]
  #before_filter :authenticate_user!

  # GET /chefs
  # GET /chefs.json
  def index
    @chefs = Chef.all
  end

  # GET /chefs/1
  # GET /chefs/1.json
  def show
  end

  # GET /chefs/new
  def new
    @chef = Chef.new
    render "chefs/registrations/new"
  end

  # GET /chefs/1/edit
  def edit
  end

  # POST /chefs
  # POST /chefs.json
  def create
    @chef = Chef.new(chef_params)

    respond_to do |format|
      if @chef.save
        format.html { redirect_to @chef, notice: 'Chef was successfully created.' }
        format.json { render action: 'show', status: :created, location: @chef }
      else
        format.html { render action: 'new' }
        format.json { render json: @chef.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /chefs/1
  # PATCH/PUT /chefs/1.json
  def update
    respond_to do |format|
      if @chef.update(chef_params)
        format.html { redirect_to @chef, notice: 'Chef was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @chef.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /chefs/1
  # DELETE /chefs/1.json
  def destroy
    @chef.destroy
    respond_to do |format|
      format.html { redirect_to chefs_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_chef
      @chef = Chef.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def chef_params
      #params[:chef]
      params.require(:chef).permit!
    end
end
class User < ActiveRecord::Base
  rolify
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable

  devise :database_authenticatable, :registerable,
      :recoverable, :rememberable, :trackable, :validatable, :omniauthable
  validates_presence_of :email
  mount_uploader :image, ImageUploader
  has_many :authorizations
  before_create :set_role

  belongs_to :meta, polymorphic: true

  def self.new_with_session(params,session)
    if session["devise.user_attributes"]
      new(session["devise.user_attributes"],without_protection: true) do |user|
        user.attributes = params
        user.valid?
      end
    else
      super
    end
  end

  def self.from_omniauth(auth, current_user)
    authorization = Authorization.where(:provider => auth.provider, :uid => auth.uid.to_s, :token => auth.credentials.token, :secret => auth.credentials.secret).first_or_initialize
    if authorization.user.blank?
      user = current_user.nil? ? User.where('email = ?', auth["info"]["email"]).first : current_user
      if user.blank?
       user = User.new
       user.password = Devise.friendly_token[0,10]
       user.name = auth.info.name
       user.email = auth.info.email
       auth.provider == "twitter" ?  user.save(:validate => false) :  user.save
     end
     authorization.username = auth.info.nickname
     authorization.user_id = user.id
     authorization.save
   end
   authorization.user
 end

    private

        def set_role
            case meta_type
            when 'Chef'
                add_role :Chef
            else
                add_role :Baer
            end
        end
end
class Chef < ActiveRecord::Base
    has_one :user, as: :meta, dependent: :destroy
    accepts_nested_attributes_for :user
end
 #app/controllers/application_controller.rb
 class ApplicationController < ActionController::Base
    def after_sign_in_path_for(resource)
         your_path
    end
 end
  def create
    build_resource(sign_up_params)

    if resource.save
      yield resource if block_given?
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_flashing_format?
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end
  end

  def sign_up(resource_name, resource)
     sign_in(resource_name, resource)
  end