Rafael Flores
Rafael Flores

Reputation: 77

Manage users through a CRUD interface with devise errors

I have establish a Crud interface with devise below my code, the problem that I am running into is that When I try to create a user a get the following errors. I can't edit either. Please help.

"2 errors prohibited this user from being saved: Email can't be blank Password can't be blank" of course I have been entering all of the information inclduding email and password. I follow the documentation from devise and have read some of this site Devise topics no such problem reported. Controller: class UsersController < ApplicationController before_filter :authenticate_user! before_action :authorized_user # GET /users # GET /users.json def index @users = User.all

  # respond_to do |format|
  #   format.html index.html.erb
  #   format.json { render :json => @users }
  end


# GET /users/1
# GET /users/1.json
def show
  @user = User.find(params[:id])

  # respond_to do |format|
  #   format.html show.html.erb
  #   format.json { render :json => @user }
  end

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

  # respond_to do |format|
  #   format.html new.html.erb
  #   format.json { render :json => @user }
  end

# GET /users/1/edit
def edit
  @user = User.find(params[:id])
end

# POST /users
# POST /users.json
def create
  @user = User.new(params[:user_params])

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

# PUT /users/1
# PUT /users/1.json
def update
  if params[:user][:password].blank?
    params[:user].delete(:password)
    params[:user].delete(:password_confirmation)
  end
  @user = User.find(params[:id])

  respond_to do |format|
    if @user.update_attributes(params[:user_params])
      format.html { redirect_to @user, :notice => 'User was successfully updated.' }
      format.json { head :ok }
    else
      format.html { render :action => "edit" }
      format.json { render :json => @user.errors, :status => :unprocessable_entity }
    end
  end
end

# DELETE /users/1
# DELETE /users/1.json
def destroy
  @user = User.find(params[:id])
  @user.destroy

  respond_to do |format|
    format.html { redirect_to users_url }
    format.json { head :ok }
  end
end


private


def user_params
  params.require(:user).permit(:username, :name, :lastname, :email, :password, :password_confirmation, :role_id)
end

def authorized_user
  if current_user.role.name == "admin"
  else
  redirect_to :root, notice: "Not authorized" if @current_user.role.name == 'user' or nil?
  end
end

end

Here is my form:

<%= 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 |msg| %>
            <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
<% end %>

<div class="field">
  <%= f.label :username %><br />
  <%= f.text_field :username, autofocus: true %>
</div>

<div class="field">
  <%= f.label :name %><br />
  <%= f.text_field :name, autofocus: true %>
</div>

<div class="field">
  <%= f.label :lastname %><br />
  <%= f.text_field :lastname, autofocus: true %>
</div>

<div class="field">
  <%= f.label :email %><br />
  <%= f.email_field :email, autofocus: true %>
</div>

<div class="field">
  <%= f.label :password %>
  <% if @minimum_password_length %>
      <em>(<%= @minimum_password_length %> characters minimum)</em>
  <% end %><br />
  <%= f.password_field :password, autocomplete: "off" %>
</div>

<div class="field">
  <%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
  <%= f.label :role_id %><br />
  <%= f.collection_select :role_id, Role.all, :id, :name_role_select  %>
</div>
<div class="actions">
  <%= f.submit "Sign up", class: 'button' %>
</div>

Upvotes: 0

Views: 553

Answers (1)

Helios de Guerra
Helios de Guerra

Reputation: 3475

You aren't properly using your user_params.

Instead of this in your create action:

User.new(params[:user_params])

You need this, which calls your user_params method which is defined in your controller:

User.new(user_params)

Your original way is looking for params passed in from your request called 'user_params'(which doesn't exist).

Similarly, for your update action you need:

if @user.update_attributes(user_params)
  #etc...

Upvotes: 1

Related Questions