允许管理员编辑用户详细信息,包括密码

时间:2015-02-23 16:27:30

标签: ruby-on-rails ruby devise rails-engines

我正在构建一个管理引擎,因此我需要有一些方法来更改用户详细信息,包括密码。

当我更改其他字段时,我的表单正在抱怨空密码。我想在不填写密码字段的情况下更改它们。

一种解决方案是"手动"检查参数并删除密码参数(如果它们是空的),但这看起来很乱。

为了让用户像资源一样对待,我可以执行CRUD操作,我将其添加到routes.rb

Admin::Engine.routes.draw do
  resources :users
end

users_controller.rb

require_dependency "admin/application_controller"

module Admin
  class UsersController < ApplicationController
    before_action :set_user, only: [:show, :edit, :update, :destroy]

    # GET /users
    def index
      @users = User.all
    end

    # GET /users/1
    def show
    end

    # GET /users/new
    def new
      @user = User.new
    end

    # GET /users/1/edit
    def edit
    end

    # POST /users
    def create
      @user = User.new(user_params)

      if @user.save
        redirect_to @user, notice: 'User was successfully created.'
      else
        render :new
      end
    end

    # PATCH/PUT /users/1
    def update
      if @user.update(user_params)
        if(@user == current_user and @user.admin_locked)
          redirect_to @user, notice: 'User was successfully updated, but you locked yourself.'
        else
          redirect_to @user, notice: 'User was successfully updated.'
        end
      else
        render :edit
      end
    end

    # DELETE /users/1
    def destroy
      @user.destroy
      redirect_to users_url, notice: 'User was successfully destroyed.'
    end

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

    # Only allow a trusted parameter "white list" through.
    def user_params
      params.require(:user).permit(:email, :role, :password, :password_confirmation, :reset_password_token, :admin_locked)
    end
  end
end

_form.html.erb

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

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

<section>
  <div class="form-group">
    <%= f.label :email %><br>
    <%= f.text_field :email, required: true%>
  </div>

  <div class="form-group">
    <%= f.label :role %><br>
    <%= f.select :role, User.roles.keys.to_a.map {|role| [role.humanize, role]} %>
  </div>

  <div class="form-group">
    <%= f.label :password, "New password" %>
    <%= f.password_field :password, autofocus: true, autocomplete: "off", class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :password_confirmation, "Confirm new password" %>
    <%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control"%>
  </div>

  <div class="form-group">
    <%= f.label :admin_locked,  'Locked'%><br>
    <%= f.check_box :admin_locked %>
  </div>
</section>

<div class="actions">
  <%= f.submit %>
</div>
<% end %>

user.rb

module Admin
  class User < ActiveRecord::Base
    validates :password, length: { in: 6..128 }, on: :update, allow_blank: true
  end
end

日志:

Started PATCH "/admin/users/3" for 127.0.0.1 at 2015-02-23 16:16:15 +0000
Processing by Admin::UsersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"OZC69AMbTAfp361HKHTYHUKnEvAI+2zf7z0k08S4a/mxHOV+IZ0r3uFxrSX2E6Euj10ZlXLK1BzKB8dsGIAEnA==", "user"=>{"email"=>"ze@ze.com", "role"=>"admin", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "admin_locked"=>"0"}, "commit"=>"Update User", "id"=>"3"}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 3]]
   (0.6ms)  BEGIN
   (0.2ms)  ROLLBACK
  Rendered admin/app/views/admin/users/_form.html.erb (3.9ms)
  Rendered admin/app/views/admin/users/edit.html.erb within layouts/admin/application (5.9ms)
  Rendered admin/app/views/layouts/admin/_navigation_links.html.erb (0.3ms)
  Rendered admin/app/views/layouts/admin/_navigation.html.erb (2.2ms)
  Rendered admin/app/views/layouts/admin/_messages.html.erb (0.3ms)
Completed 200 OK in 55ms (Views: 21.0ms | ActiveRecord: 3.6ms)

版本:Rails 4.2,设计3.4.1

请仔细阅读评论:这不是典型的Rails应用,它发生在Rails引擎中。如果你投票结束请理解差异。

0 个答案:

没有答案