1

I have been trying to update a user from the admin, but when updating a user asks me not to leave the password field blank and confirm password, how can I fix this?

PD: I'm using devise for the users, this only happens when I print users inside the administrator and try to update a user record

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 Answers1

0

I can fix the problem with this link of devise: https://github.com/plataformatec/devise/wiki/How-To%3a-Manage-users-through-a-CRUD-interface.

Devise is great because of its flexibility. Performing basic CRUD actions on the User model is actually no different than creating CRUD actions for anything else in Rails.

There are a couple things to remember though:

Make sure to put your resources :users below the devise_for :users route.

Since the registration routes and user managing routes can conflict, you need to change one of them. You can either put devise under a specific prefix:

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

Or your users:

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

In your UsersController, you will also need to remove the password key of the params hash if it's blank. If not, Devise will fail to validate. Add something like this to your update method:

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

Examples of CRUD Actions in UsersController:

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

I hope you can also be very useful! =D

karel
  • 5,489
  • 46
  • 45
  • 50