在rails app中为用户添加角色?

时间:2016-12-27 00:24:41

标签: ruby-on-rails

总结:我希望有两种类型的用户,即教师和学生。它们都是用户,因此从用户控制器继承。问题不在于现有的控制器或类,它是如何通过表单调整它们以及我是否遗漏任何东西。我不确定如何做一个后端到单选按钮,并尝试过以前的答案,但它们会导致错误。另外,如何根据选择的角色更改路线?我有一个注册控制器来覆盖Devise。

这是我的表格:

<div class="authform">   <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :role => 'form'}) do |f| %>
    <h2>Sign Up</h2>
    <%= devise_error_messages! %>
    <div class="form-group">
      <%= f.label :name %>
      <%= f.text_field :name, :autofocus => true, class: 'form-control' %>
    </div>
    <div class="form-group">
      <%= f.label :email %>
      <%= f.email_field :email, class: 'form-control' %>
    </div>
    <div class="form-group">
      <%= f.label :password %>
      <%= f.password_field :password, class: 'form-control' %>
    </div>
    <div class="form-group">
      <%= f.label :password_confirmation %>
      <%= f.password_field :password_confirmation, class: 'form-control' %>
    </div>
    <div>
      <%= f.radio_button :role, 'student' %>
      <%= label :role_student, 'Student' %>

      <%= f.radio_button :role, 'teacher' %>
      <%= label :role_teacher, 'Teacher' %>
    </div>

    <%= f.submit 'Sign Up', :class => 'button right' %>
        <% end %> </div>

我有一个迁移,可以在这里为用户添加角色

class AddRoleToUsers < ActiveRecord::Migration
  def change
    add_column :users, :role, :integer
  end
end

这是我在添加角色时更改路径的原始尝试(来自另一个堆栈溢出问题)

class RegistrationsController < Devise::RegistrationsController

  def after_sign_up_path_for(resource)
    if params[:user][:role] == 'student'
      path_to_route = '/classrooms/index'
    elsif [:user][:role] == 'teacher'
      path_to_route = '/classrooms/new'
    else
      path_to_route = '/'
    end
    redirect_to path_to_route
  end

end

最后,这是我的用户控制器

class UsersController < ApplicationController
  # before_action :authenticate_user!
  before_action :logged_in_user, only: [:edit, :update]
  before_action :correct_user,   only: [:edit, :update]
  after_action :verify_authorized
  # before_filter :check_role

  def show
    @user = User.find(params[:id])
    authorize @user
    @posts = @user.posts
  end

  def new
    @user = User.new
    authorize @user
  end

  #def after_sign_up_path_for(resource)
  #  if params[:user][:role] == 'student'
  #    path_to_route = '/classrooms/index'
  #  elsif [:user][:role] == 'teacher'
  #    path_to_route = '/classrooms/new'
  #  else
  #    path_to_route = '/'
  #  end
  #  redirect_to path_to_route
  #end


  def update
    @user = User.find(params[:id])
    authorize @user
    if @user.update_attributes(secure_params)
      redirect_to users_path, :notice => "User updated."
    else
      redirect_to users_path, :alert => "Unable to update user."
    end
  end

  def destroy
    user = User.find(params[:id])
    # authorize @user
    user.destroy
    redirect_to users_path, :notice => "User deleted."
  end

  def create_user_type
  end 

  private

  def secure_params
    params.require(:user).permit(:role)
  end

  def logged_in_user
      unless logged_in?
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
  end

  def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless @user == current_user
  end
end

我知道这是一个很长的帖子,但我很感激任何帮助。非常感谢!我喜欢rails社区。

1 个答案:

答案 0 :(得分:2)

如果您使用simple_form,则可以使用标准输入并将选项分配给<%= f.select(:role, User.roles.keys.map {|role| [role.titleize,role]}) %>

with simple_form

enum role: [:admin, :subscriber, :poster]

没有simple_form

{{1}}

user.rb

{{1}}