Question

I have a rails app using devise for registrations and rolify for roles. I would like to have an index page that has edit links for each of the users that can be accessed by an admin. This edit page should also work without having to use a password. Right now the edit_user_path goes to the edit page of the current user, which is not what i want.

What is the best way to implement this sort of sitation? i've read a few of the posts on here about this but none seem to give me what i want.

Please point me in the right direction!

EDITED I'm attempting to do it this way, still running into "Current password can't be blank" From Users_controller:

    def update
    @user = User.find(params[:id])

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

    if @user.update_attributes(user_params)
      redirect_to users_path, :notice => "User updated."
    else
      redirect_to users_path, :alert => "Unable to update user."
    end
  end

And in my views i have an edit.html.erb file that is rendering the following form:

<div class="panel-body">
    <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
      <%= devise_error_messages! %>

      <div class="form-group">
        <%= f.label :email %>
        <%= f.email_field :email, class: "form-control", :autofocus => true %>
      </div>

      <div class="form-group">
        <%= f.label :username %>
        <%= f.text_field :username, class: "form-control" %>
      </div>

      <div class="form-group">
        <%= f.label :firstname %>
        <%= f.text_field :firstname, class: "form-control" %>
      </div>

      <div class="form-group">
        <%= f.label :lastname %>
        <%= f.text_field :lastname, class: "form-control" %>
      </div>

      <div class="form-group">
        <%= f.label :city %>
        <%= f.text_field :city, class: "form-control" %>
      </div>

      <div class="form-group">
        <%= f.label :zip %>
        <%= f.text_field :zip, class: "form-control" %>
      </div>

      <div class="form-group">
        <%= f.label :state %>
        <%= f.text_field :state, class: "form-control" %>
      </div>

      <div class="form-group">
        <%= f.label :country %>
        <%= f.text_field :country, class: "form-control" %>
      </div>

      <div class="form-group">
        <%= f.submit "Update", class: "btn btn-primary" %>
      </div>
    <% end %>
  </div>

and finally in my routes.rb file i have this line to render the edit page. I can get the edit page to show up but entering info and then hitting update just shoots me to /users with the error "Current Password can't be blank"

get 'pressroom/accounts/:id/edit' => 'users#edit', :as => :admin_edit_user
Was it helpful?

Solution

Devise doesn't come with any sort of Admin interface. If you are the only administrator and don't mind a little crudeness - there is always the console and/or scaffolding. You could create a UserController which inherits from ApplicationController and execute basic view, edit methods in the same controller. By placing the appropriate new.html.erb, edit.html.erb etc files in the User Views folder, adding/editing/deleting Users should work no differently as any other CRUD, as Devise's User is another model like any. Use a scaffold on the user and you could get what you are looking for.

There are also a lot of good gems that make setting up admin interfaces a cinch: https://github.com/gregbell/active_admin Active Admin, https://github.com/sferik/rails_admin Rails Admin and I'm sure there are a bunch more out there.

OTHER TIPS

It looks like i got it working by adding:

<div class="panel-body">
<% @user = User.find(params[:id]) %>

    <%= form_for(@user) do |f| %>

to the top of my _form.html.erb file

Thanks for the help everyone!

If the only thing you need is for the admin to EDIT an existing user, you can have the edit, show and update actions in a separate UsersController (and leave new and create actions up to devise). That way you can move that @user = User.find(params[:id] logic out of your form, into the controller, as @Saurabh Lodha mentioned.

I just thought one thing was missing from the answers though: Make sure to also edit your routes.rb. Use a path prefix so your routing doesn't get confusing, kind of like this:
devise_for :users, :path_prefix => 'my' resources :users

this means that when you call edit on a current_user, it will go to my/users/edit, and when you call edit on any selected user from your user list in the admin panel, it will take you to users/user_id/edit.

I hope that clarified it a bit more! good luck! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top