2016-11-29 113 views
0

我是Rails的新手,并且正在建立一个工作板marketplace.I'm卡住了用户注册配置文件表单。我在Rails 5中使用了devise(4.2)。我在user_create之前创建了配置文件,然后只是重定向到配置文件视图,现在使用注册表单字段(如first_name & last_name)更新用户。Rails 5 - 在设计sign_up后清空用户配置文件

我的注册是一个双向过程,而不是嵌套的配置文件。 第1步 - 设计注册表格,只需要一个额外的字段来检查用户作为用户(求职者)或公司的角色。 第2步 - 基于此角色,为用户和公司创建单独的配置文件表单。如果配置文件未提交给数据库,则用户也不应保存,并且必须再次发生用户sign_up。

目前,我只用一个通用的用户表单,它具有first_name和last_name字段。我在用户模型中使用了before_create:build_profile,但它只是传递Profile.create而不在数据库上创建它。我应该在profiles_controller还是registrations_controller(设计)中覆盖'create'或'new'。

views/profiles/show.html.erb中的错误是:表单中的第一个参数不能包含零或为空。在终端上,我看到这个配置文件:“user_id”=>“2”,“id”=>“profile_id”} ...这是否表示DB中的用户和配置文件表之间的东西阻碍了创建或路由正确设置?

的routes.rb

Rails.application.routes.draw do 
    root "jobs#index" 
    devise_for :users, :controllers => {registrations: "registrations"} 
    resources :users do 

    resources :profiles, only: [:show, :update] 
    end 
end 

Registrations_controller

class RegistrationsController < Devise::RegistrationsController 
    protected 
    def after_sign_up_path_for(user) 
     if resource.class == User && current_user.role == 'user' 
     user_profile_path 
     else # to check for company user resource.class == User && current_user.role == 'company' 
     puts "redirecting to company profile-will do later" 
     end 
    end 
    end 

profiles_controller.rb

 class ProfilesController < ApplicationController 

     def show 
     end 

     def update 
     @profile = Profile.find_by_user_id(profile_params) 
     current_user.update(@profile) 
     end 

     def destroy 
     @profile.destroy 
     respond_with(@profile) 
     end 

     private 

     def profile_params 
     params.require(:user).permit(profile_attributes: []) 
     end 

     def set_profile 
     @profile = Profile.find(params[:id]) 
     end 
    end 

profile.rb

class Profile < ApplicationRecord 
    belongs_to :user 
end 

user.rb

class User < ApplicationRecord 
     before_create :build_profile 
     devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable 
    enum role: [:user, :company, :admin] 
     has_one :profile 

     def build_profile 
     Profile.create 
     true 
     end 

回答

0

我建议你使用基于继承的方法。

您可以在用户表中添加一个类型,Rails会为您进行管理。

example_timestamp_add_type_to_users.rb

class AddTypeToUsers < ActiveRecord::Migration[5.0] 
    def change 
    add_column :users, :type, :string, null: false 
    end 
end 

使用这个,你可以添加为用户类型,只要你想:

型号/ job_seeker.rb

class JobSeeker < User 
    has_one :job_seeker_profile, dependent: :destroy 
    after_create :profile 

    private 

    def profile 
    create_job_seeker_profile 
    end 
end 

型号/ company_owner .rb

class CompanyOwner < User 
    has_one :company_owner_profile, dependent: :destroy 
    after_create :profile 

    private 

    def profile 
    create_company_owner_profile 
    end 
end 

models/admin。RB

class Admin < User 
end 

这种做法将允许您使用基于用户类型的身份验证方法:

求职者:

  1. current_job_seeker
  2. authenticate_job_seeker!

,公司的所有者:

  • current_company_owner
  • authenticate_company_owner!
  • 为管理员:

  • current_admin
  • authenticate_admin!
  • 还可以构建不同的注册和/或登录表格为每个用户类型:

    配置/ routes.rb中

    Rails.application.routes.draw do 
        devise_for :admins 
        devise_for :job_seekers 
        devise_for :company_owners 
    end 
    
    +0

    感谢但实际上延伸到配置文件和用户公司是下一个过程现在配置文件没有被创建。 – Means