Zac*_*ams 2 ruby authentication ruby-on-rails devise ruby-on-rails-4
我正在构建一个工作委员会应用程序。我是编程新手,正在自学 Rails 框架。
我正在使用 Devise 进行身份验证。我将有两种不同类型的用户;Job Seeker和Employer。求职者将创建个人资料并搜索职位发布,雇主将创建公司简介并发布职位列表。将来,雇主还可以根据资格、经验、教育程度等来寻找员工,但现在我只是在构建我的 MVP。
这种类型的功能很棘手,即因为您必须在实现之前放置功能(即大多数人对此感到困惑Devise,而它可能根本没有功能)
你有两种方法:
Devise是一个认证系统(用户登录);您可能更好地使用授权(用户可以执行 x 或 y)。授权超出了 Devise 的范围。
虽然您可以使用多个模型(Devise),但我认为它会为您的需求带来太多不必要的膨胀。
相反,我会使用一个非常简单的角色系统(使用enum):
#app/models/user.rb
class User < ActiveRecord::Base
enum role: [:job_seeker, :employer]
has_one :profile
before_create :build_profile
has_many :applications
has_many :listings, through: :applications
end
#app/models/application.rb
class Application < ActiveRecord::Base
belongs_to :listing
belongs_to :user
end
#app/models/listing.rb
class Listing < ActiveRecord::Base
has_many :applications
has_many :applicants, through: :applications, class_name: "User", foreign_key: :user_id
end
Run Code Online (Sandbox Code Playgroud)
您需要向表中添加一role列 ( int) users。您将在创建列时使用开关创建默认角色:default: [x]
def change
add_column :users, :role, :integer, default: 0 #-> defaults to job seeker
end
Run Code Online (Sandbox Code Playgroud)
--
您已经描述了几个非常适合此目的的因素:
Job seeker将创建一个个人资料Employer将创建个人资料并发布列表
...这意味着两种用户类型的“流程”将保持相似。您只需管理每个用户可以通过授权执行哪些操作。
#config/routes.rb
resource :profile, controller: :users, only: [:show, :update] #-> url.com/profile
resources :listings, only: [:show] do
post :apply, on: :member #-> url.com/listings/:id/apply
end
resources :companies, controller: :users, only: [:show]
#app/controllers/users_controller.rb
class UsersController < ApplicationController
#show will automatically be loaded
def update
current_user.update profile_params
end
private
def profile_params
params.require(:user).permit(profile_attributes: [:name, :etc, :etc])
end
end
#app/views/users/show.html.erb
<%= form_for current_user do |f| %>
<%= f.fields_for :profile do |p|
<% if current_user.job_seeker? %>
<%= f.text_field :name, placeholder: "Your name" %>
<% elsif current_user.employer? %>
<%= f.text_field :name, placeholder: "Company name" %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用以下命令来检查用户是否可以创建列表,或者只是查看:
#app/controllers/listings_controller.rb
class ListingsController < ApplicationController
before_action :check_seeker, only: [:apply]
before_action :check_employer, only: [:new, :create, :destroy]
def new #-> employers
@listing = current_user.listings.new
end
def apply #-> job seekers
@listing = Listing.find params[:id]
@application = current_user.applications.new
@application.listing = @listing
redirect_to @listing, notice: "Application successful!" if @application.save
end
private
def check_seeker
redirect_to listings_path, notice: "Only Job Seekers Allowed" unless current_user.job_seeker?
end
def check_employer
redirect_to root_url, notice: "Only Employers Allowed" unless current_user.employer?
end
end
Run Code Online (Sandbox Code Playgroud)
希望这能给您要点。
要让 Devise 使用您的新色谱柱,您需要扩展 Devise Sanitizer:
#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :role
end
end
Run Code Online (Sandbox Code Playgroud)
这将允许您更改role注册时的字段:
#app/views/devise/registrations/new.html.erb
.....
<%= f.select :role, User.roles %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1759 次 |
| 最近记录: |