简体   繁体   English

如何在不使用Ruby on Rails的设计的情况下添加新的管理员用户

[英]How to add new admin user without using of devise in Ruby on Rails

I am new in Ruby on Rails and I am using Ruby 1.9.3 andRails 4.0.2. 我是Ruby on Rails的新手,我使用的是Ruby 1.9.3 andRails 4.0.2。

How can I add a new admin user without using of devise? 如何在不使用设计的情况下添加新的管理员用户?

I am using below code and submit add new user form with data admin_user_params returns a blank and a blank record is inserted in database. 我使用下面的代码并提交添加新的用户表单,数据admin_user_params返回一个空白,并在数据库中插入一个空白记录。

This my user db table 这是我的用户数据库表

create_table :users do |t|
  t.integer :role_id
  t.string :name
  t.string :email
  t.string :username
  t.string :encrypted_password
  t.string :reset_password_token
  t.string :is_status
  t.string :is_active
  t.integer :sign_in_count
  t.string :current_sign_in_ip
  t.string :last_sign_in_ip
  t.string :authentication_token
  t.datetime :reset_password_sent_at
  t.datetime :remember_created_at
  t.datetime :current_sign_in_at
  t.datetime :last_sign_in_at

this is my controller 这是我的控制器

class Admin::UsersController < ApplicationController

  before_action :set_admin_user, only: [:show, :edit, :update, :destroy]

  def new
    @admin_user = User.new
  end



 def create
    #@admin_user = Admin::User.new(admin_user_params)
    @admin_user = User.new(admin_user_params)

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

* This is my User Model * * 这是我的用户模型*

Note: 注意:

I don't want to create admin_user model. 我不想创建admin_user模型。 I want to using only User model for admin area and fronted area. 我想只使用用户模型管理区域和前端区域。

class User < ActiveRecord::Base

  #Setup accessible (or protected) attributes for your model
  attr_accessible :name, :username, :role_id, :email, :password, :password_confirmation, :remember_me, :id, :admin_permission
  attr_accessor :password, :new_password, :previous_email, :previous_username, :remember_me

  belongs_to :role

  def super_admin?
   self.role.name == "Super Admin"
  end
end

This is my View 这是我的观点

<%= form_for [:admin,@admin_user]  do |f| %>
  <% if @admin_user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@admin_user.errors.count, "error") %> prohibited this admin_user from being saved:</h2>

      <ul>
      <% @admin_user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :Name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :Email %><br>
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :Username %><br>
    <%= f.text_field :username %>
  </div>
  <div class="field">
    <%= f.label :Password %><br>
    <%= f.text_field :encrypted_password %>
  </div>
  <div class="actions">
    <p><%= f.submit 'Sign up'  %></p>
  </div>
 <% end %> 

Please Help. 请帮忙。

I think the easiest way to add admin user functionality is to add a boolean admin row to your users table. 我认为添加管理员用户功能的最简单方法是在users表中添加一个布尔admin行。

Then, in your controllers, you can control access to specific actions with a before_action that ensures the user is an admin, or else redirects to root_url (or wherever). 然后,在您的控制器中,您可以使用before_action控制对特定操作的访问,以确保用户是管理员,或者重定向到root_url (或任何地方)。

You can also selectively view elements in your views by calling User#admin? 您还可以通过调用User#admin?来有选择地查看视图中的元素User#admin? in a conditional. 在有条件的。

Not sure if that meets your needs, but I hope it helps. 不确定这是否符合您的需求,但我希望它有所帮助。

A nice flexible way to do this would be to use Rolify + Authority . 一个很好的灵活方法是使用Rolify + Authority This will allow you to assign admin roles to users in addition to other roles. 除了其他角色之外,这还允许您为用户分配管理员角色。 From there you can control access to specific methods based on those roles. 从那里,您可以根据这些角色控制对特定方法的访问。

The roles are applied to the users by associations, so you can ask questions like 角色通过关联应用于用户,因此您可以提出类似的问题

@user.has_role? :admin  

This can be scoped globally, to models or to instances of models. 这可以是全局范围,模型或模型实例。 Fine grain control while being easy to use. 细粒度控制,易于使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM