从管理Rails更新用户时出现问题

时间:2016-12-27 01:47:45

标签: ruby-on-rails devise

我一直在尝试从管理员更新用户,但在更新用户时要求我不要将密码字段留空并确认密码,我该如何解决?

PD:我正在为用户使用设计,这只有在我在管理员中打印用户并尝试更新用户记录时才会发生

enterprise.rb

class Enterprise < ApplicationRecord

   validates :name, :about, :address, :image, :municipality_id, :enterprise_tag_id, presence: true
   validates :name, uniqueness: true

end

enterprises_controller.rb

class EnterprisesController < ApplicationController

  def index
    @enterprise_tags = EnterpriseTag.all
      if params[:enterprise_tag_id].present?
        @enterprises = Enterprise.paginate(:page => params[:page], :per_page => 8).by_category(params[:enterprise_tag_id])
      else
        @enterprises = Enterprise.paginate(:page => params[:page], :per_page => 8)
      end
   end

  def show
    @enterprise = Enterprise.find(params[:id])
    @hash = Gmaps4rails.build_markers(@enterprise) do |enterprise, marker|
      marker.lat enterprise.latitude
      marker.lng enterprise.longitude
    end
  end

  def new
  end

  def edit
    @enterprise = Enterprise.find(params[:id])
  end

  def create
  end

  def update
    @enterprise = Enterprise.find(params[:id])

    respond_to do |format|
      if @enterprise.update(enterprise_params)
        format.html { redirect_to admin_dashboard_path, notice: 'La empresa fue actualizada' }
        format.js {}
        format.json { render :show, status: :ok, location: @enterprise }
      else
        format.html { render :edit }
        format.js {}
        format.json { render json: @enterprise.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @enterprise = Enterprise.find(params[:id])
    @enterprise.destroy
    redirect_to admin_dashboard_path
  end

private

  def enterprise_params
    params.require(:enterprise).permit(:name, :password, :password_confirmation, :municipality_id, :enterprise_tag_id, :email, :address, :image, :about)
  end

end

_form.html.erb

<div class="callout">
  <%= form_for @enterprise, remote: true, authenticity_token: true, html: { multipart: true } do |f| %>

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

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

    <div class="row">
      <div class="small-6 columns">
        <%= f.label :nombre %>
        <%= f.text_field :name, autofocus: true, placeholder: "Nombre de la empresa", required: true %>
      </div>
      <div class="small-6 columns">
        <%= f.label :correo_electronico %>
        <%= f.email_field :email, placeholder: "Correo electronico", required: true %>
      </div>
    </div>

    <div class="row">
      <div class="small-6 columns">
        <%= f.label :contraseña %>
        <%= f.password_field :password, autocomplete: "off", required: true %>
      </div>
      <div class="small-6 columns">
        <%= f.label :password_confirmation %>
        <%= f.password_field :password_confirmation, autocomplete: "off", required: true %>
      </div>
    </div>

    <div class="row">
      <div class="small-6 columns">
        <%= f.label :categoria %>
        <%= f.collection_select :enterprise_tag_id, EnterpriseTag.all, :id, :name, { :include_blank => "Seleccionar categoria"}, required: true %>
      </div>
      <div class="small-6 columns">
        <%= f.label :municipio %>
        <%= f.collection_select :municipality_id, Municipality.all, :id, :name, { :include_blank => "Seleccionar municipio"}, required: true %>
      </div>
    </div>

    <div class="row">
      <div class="small-12 columns">
        <%= f.label :dirección %>
        <%= f.text_field :address, placeholder: "Ej. 6ta Calle Ote, Usulutan", required: true %>
      </div>
    </div>

    <div class="row">
      <div class="small-12 columns">
        <%= f.label :logo_representativo_de_la_empresa %>
        <div class="input-group">
          <%= f.text_field :image, disabled: true, class: "input-group-field" %>
          <div class="input-group-button">
            <label for="exampleFileUpload" class="button">Agregar logo</label>
            <%= f.file_field :image, id: "exampleFileUpload", class: "show-for-sr" %>
          </div>
        </div>
      </div>
    </div>

<br>

    <div class="row">
      <div class="small-12 columns">
        <%= f.label :acerca_de_la_empresa %>
        <%= f.text_area :about, placeholder: "Acerca de la empresa", :rows => 4, :cols => 120, required: true %>
      </div>
    </div>

    <div class="row">
      <div class="small-12 columns">
        <%= f.submit "Actualizar datos", class: "button" %>
      </div>
    </div>

  <% end %>
</div>

1 个答案:

答案 0 :(得分:0)

我可以用这个设计链接解决问题: https://github.com/plataformatec/devise/wiki/How-To%3a-Manage-users-through-a-CRUD-interface

由于其灵活性,设计很棒。在用户模型上执行基本的CRUD操作实际上与在Rails中为其他任何内容创建CRUD操作没有什么不同。

但要记住几件事:

请务必将resources :user放在devise_for :users路线下方。

由于注册路由和用户管理路由可能会发生冲突,因此您需要更改其中一个。您可以将设计放在特定前缀下:

devise_for :users, :path_prefix => 'my'
resources :users

或您的用户:

devise_for :users
scope "/admin" do
  resources :users
end

在您的UsersController中,如果它是空白的,您还需要删除params哈希的密码密钥。如果没有,Devise将无法验证。在您的更新方法中添加以下内容:

if params[:user][:password].blank?
  params[:user].delete(:password)
  params[:user].delete(:password_confirmation)
end

UsersController中的CRUD操作示例:

def destroy
  @user.destroy!

  respond_to do |format|
    format.json { respond_to_destroy(:ajax) }
    format.xml  { head :ok }
    format.html { respond_to_destroy(:html) }      
  end
end

我希望你也能非常有用! = d