Add 'index' action to registrations controller

373 views
Skip to first unread message

Javix

unread,
Mar 6, 2012, 10:14:50 AM3/6/12
to Devise
I can't figure out how to add an 'index' action to the registrations
controller to be able to list all the users.
So I created a new controler as described in the WiKi:
```
#in app/controllers/users
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :check_permissions, :only =>
[:new, :create, :cancel, :index]
skip_before_filter :require_no_authentication

def check_permissions
authorize! :create, resource
end

def index
@users = User.all
end

def edit
@user = User.find(current_user.id)
end

def update
@user = User.find(current_user.id)
params[:user].delete(:password) if params[:user][:password].blank?
params[:user].delete(:password_confirmation) if params[:user]
[:password].blank? and params[:user][:password_confirmation].blank?
if @user.update_attributes(params[:user])
flash[:notice] = "Successfully updated User."
redirect_to root_path
else
render :action => 'edit'
end
end
end
```
Then defined in routes.rb like that:
```
#routes.rb
devise_for :users, :controllers => { :registrations => "users/
registrations" } do
get 'users/list' => 'users/registrations#index', :as
=> :users_list
end
```
But when checking the available routes with rake routes, I don't see
the index root:
```
users_list GET /users/list(.:format) users/
registrations#index
new_user_session GET /users/sign_in(.:format) devise/
sessions#new
user_session POST /users/sign_in(.:format) devise/
sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/
sessions#destroy
user_password POST /users/password(.:format) devise/
passwords#create
new_user_password GET /users/password/new(.:format) devise/
passwords#new
edit_user_password GET /users/password/edit(.:format) devise/
passwords#edit
PUT /users/password(.:format) devise/
passwords#update
cancel_user_registration GET /users/cancel(.:format) users/
registrations#cancel
user_registration POST /users(.:format) users/
registrations#create
new_user_registration GET /users/sign_up(.:format) users/
registrations#new
edit_user_registration GET /users/edit(.:format) users/
registrations#edit
PUT /users(.:format) users/
registrations#update
DELETE /users(.:format) users/
registrations#destroy
```
What I did wrong? Thanks

José Valim

unread,
Mar 6, 2012, 11:45:18 AM3/6/12
to plataforma...@googlegroups.com
Which Devise version?

Notice that if you are using Devise 2.0, passing a block to devise_for is deprecated.

Sergueï Cambour

unread,
Mar 6, 2012, 11:56:53 AM3/6/12
to plataforma...@googlegroups.com
Sorry, José, I forgot to point it out.

I'm on Windows box, devise is of 2.0.4, ruby is 1.9.2, rails 3.2.1

I modified the controller's cod as follows:


class Users::RegistrationsController < Devise::RegistrationsController
  before_filter :check_permissions, :only => [:new, :create, :cancel, :index]
  skip_before_filter :require_no_authentication
 
  def check_permissions
    authorize! :create, resource
  end
 
  def index
    @users = User.all
  end
 
  def edit           
    @user = User.find(params[:id])
    #@user = User.find(current_user.id)
  end
 
  def update   
    @user = User.find(params[:id])
    #@user = User.find(current_user.id)

    params[:user].delete(:password) if params[:user][:password].blank?
    params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank?
    if @user.update_attributes(params[:user])
      flash[:notice] = "Successfully updated User."
      redirect_to root_path
    else
      render :action => 'edit'
    end
  end
end

And the routes as follows:


devise_for :users, :controllers => { :registrations => "users/registrations" }   
  devise_scope :user do   
    get 'users/index', :to => 'users/registrations#index', :as => :users_list
  end

Now the users list works. But another problems comes, - customize edit and update actions to be able to update a User selected from the displayed Users list like in a usual CRUD way. What is the best solution, up to you? Should I create a separate edit and  new pages for my Users::Registrations controller what is not DRY for me (the code is really the same as the devise's one). The only difference is to get a User id ti modifiy in the controller instead of the id of the current User.

Thank you

Javix

unread,
Mar 7, 2012, 6:07:04 AM3/7/12
to Devise
How to pass the user id from displayed list to personalize 'edit'
action in RegistrationsController ?By default it takes the
current_user.id.

```
class Users::RegistrationsController < Devise::RegistrationsController

def edit
@user = User.find(params[:id])
#@user = User.find(current_user.id)
end

def update
@user = User.find(params[:id])
#@user = User.find(current_user.id)
params[:user].delete(:password) if params[:user][:password].blank?
params[:user].delete(:password_confirmation) if params[:user]
[:password].blank? and params[:user][:password_confirmation].blank?
if @user.update_attributes(params[:user])
flash[:notice] = "Successfully updated User."
redirect_to root_path
else
render :action => 'edit'
end
end
```
In the devise/registrations.index.html.erb:
```
...
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.admin? %></td>
<td><%= link_to 'Edit', edit_user_registration_path(:id=> user.id)
%></td>
</tr>
<% end %>
```
> On 6 March 2012 17:45, José Valim <jose.va...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Which Devise version?
>
> > Notice that if you are using Devise 2.0, passing a block to devise_for is
> > deprecated.
>
> > On Tuesday, March 6, 2012 4:14:50 PM UTC+1, Javix wrote:
>
> >> I can't figure out how to add an 'index' action to the registrations
> >> controller to be able to list all the users.
> >> So I created a new controler as described in the WiKi:
> >> ```
> >> #in app/controllers/users
> >> class Users::RegistrationsController < Devise::**RegistrationsController
> >>   before_filter :check_permissions, :only =>
> >> [:new, :create, :cancel, :index]
> >>   skip_before_filter :require_no_authentication
>
> >>   def check_permissions
> >>     authorize! :create, resource
> >>   end
>
> >>   def index
> >>     @users = User.all
> >>   end
>
> >>   def edit
> >>     @user = User.find(current_user.id)
> >>   end
>
> >>   def update
> >>     @user = User.find(current_user.id)
> >>     params[:user].delete(:**password) if params[:user][:password].**blank?
>
> >>     params[:user].delete(:**password_confirmation) if params[:user]
> >> [:password].blank? and params[:user][:password_**confirmation].blank?
> >>     if @user.update_attributes(**params[:user])
> >>       edit_user_password GET    /users/password/edit(.:**format) devise/

Javix

unread,
Mar 7, 2012, 7:08:53 AM3/7/12
to Devise
I can display the edit page for the selected user, but can't update
it.
I tried to modify the edit page as follows:
```
<%= form_for(resource, :as => resource_name, :url =>
registration_path(resource_name), :html => { :method => :put, :id =>
"edit#{resource_name}_#{resource.id}" }) do |f| %>
```
but in 'updaet action there is still no ID in params.
Any idea?

Javix

unread,
Mar 7, 2012, 7:36:25 AM3/7/12
to Devise
I don't know if it is correct, but it works when defined as follows:

#in devise/registrations.edit.html.erb:

<%= form_for(resource, :as => resource_name, :url =>
registration_path(resource_name, :id=> resource.id), :html =>
{ :method => :put}) do |f| %>
...

and I had to add current_password attribute accessor to the User
model:

#models/user.rb
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :admin, :current_password
attr_accessor :current_password
...
end
Reply all
Reply to author
Forward
0 new messages