I am using devise for my users. I recently installed the rails Active Admin gem, everything is working beautifully.
However I can't figure out how to add a new admin users. I can see that active admin created an admin_user table in the db with a user admin#example.com, which I use to log in to the interface.
I tried adding admin_user as a resource so that I can just click the Add Admin User button within the active admin interface to add a new user, however that does not seem to work.
What brian said works perfectly
http://net.tutsplus.com/tutorials/ruby/create-beautiful-administration-interfaces-with-active-admin/
AdminUser.create!(:email => 'admin#example.com', :password => 'password', :password_confirmation => 'password')
What Brian said works, but if you want to set the password in the interface rather than have it send a reset email try this:
Leave the admin_user model at its original generated default, then in app/admin/admin_users.rb:
ActiveAdmin.register AdminUser do
index do
column :email
column :current_sign_in_at
column :last_sign_in_at
column :sign_in_count
default_actions
end
form do |f|
f.inputs "Admin Details" do
f.input :email
f.input :password
f.input :password_confirmation
end
f.buttons
end
end
login: admin#example.com password: password => login
go to http://localhost:3000/admin/admin_users
If you want create users (devise users, table "users") in admin panel:
$ rails generate active_admin:resource user
app/admin/user.rb:
ActiveAdmin.register User do
permit_params :email, :name, :password, :password_confirmation
index do
column :name
column :email
actions
end
form do |f|
f.inputs 'User' do
f.input :name
f.input :email
f.input :password
f.input :password_confirmation
end
f.actions
end
end
this is the new syntax
AdminUser.create!(email: "youremail#domain.com", password: "password123", password_confirmation: "password123")
Related
In rails 6, I am using activeadmin gem for admin interface.
My code is like,
ActiveAdmin.register Account, as: "Individual Accounts" do
menu parent: "Accounts", label: "Individual Accounts"
permit_params :first_name, :last_name, :email, :password, :role_id
filter :first_name
filter :email
index do
selectable_column
column :first_name
column :last_name
column "Role" do |acc|
acc.role
end
column :email
actions
end
form do |f|
f.inputs do
f.input :first_name
f.input :last_name
f.input :email
f.input :password
end
f.actions
end
show do
attributes_table do
row :role
row :first_name
row :last_name
row :email
end
end
controller do
after_create :set_account_type
def set_account_type resource
resource.type = "EmailAccount"
role = RolesPermissions::Role.find_by(name:"Individual")
resource.role_id = role.id
resource.save
end
def create
super do |success,failure|
success.html { redirect_to admin_individual_accounts_path }
end
end
end
controller do
def scoped_collection
Account.joins(:role).where(roles: {name: "Individual"})
end
end
end
I want to remove New Individual Accounts button(option) from the page.
If I try to override an action_item like as mentioned below then one more button will get add
action_item only: :new do
link_to 'Add New', new_admin_individual_account_path
end
Please help me to remove new option.
I am able to add new users in ActiveAdmin. I can view it as an encrypted password string but when I edit it, the password fields are blank.
I know that they remain unchanged but UX-wise, I need to show the person editing the user that a password is already in place. I'm using devise validation thus the encrypted password.
How can I do this?
So just an update if someone might need this in the future.
Since the you can't normally place back an encrypted password back on the password field for update, I figured the other workaround is to be able to HIDE the two password fields on update and have a separate action for it that only the user can actually change the password through his/her email.
In ActiveAdmin I wrote the form like this:
form do |f|
f.inputs 'Admin Details' do
f.input :email
if f.object.new_record?
f.input :password, as: :password
f.input :password_confirmation, :label => "Password Confirmation"
end
end
f.actions
end
The roadblock for this solution is the validation. I must be able to allow the user to update the other fields without the need to update the password everytime. It's not a perfect solution but I only validate their presence on create.
validates :password,
presence: {
:message => 'Password cannot be blank'
},
:confirmation => true,
on: :create
validates :password,
:confirmation => {
case_sensitive: true
},
:length => {
:within => 8..128,
too_short: "Password is too short (minimum is 8 characters)",
too_long: "Password is too long (maximum is 128 characters)"
},
:unless => lambda{ |adminuser| adminuser.password.blank? },
on: :create
validates :password_confirmation,
presence: {
:message => 'Field cannot be blank'
},
on: :create
So yeah, it's sending change password instructions through email by passing a token
I revised an answer from this and got it working like this
# app/admin/inputs/readonly_input.rb
class ReadonlyInput < Formtastic::Inputs::StringInput
def to_html
input_wrapping do
label_html <<
template.content_tag('div', #object.send(method))
end
end
end
# app/admin/admin_users.rb
ActiveAdmin.register AdminUser do
# ...
form do |f|
f.semantic_errors
f.inputs 'Admin Details' do
f.input :email
f.input :encrypted_password, label: "Current Password", as: :readonly
f.input :password
f.input :password_confirmation
end
f.actions
end
end
I am using ActiveAdmin as a admin panel, so I can create users through ActiveAdmin.
The Issue I'm having is when updating or inserting a users password, I need the value I entered in the ActiveAdmin form to hash the password with bcrypt and then work with rails has_secure_password authentication
Is there anyway I can get ActiveAdmin to include something like this?
BCrypt::Password.create(params[:password])
before saving to the database?
this is my users.rb
ActiveAdmin.register User do
permit_params :email, :password_digest, :session_token, :session_key,
:rank, :profileColour
index do
selectable_column
id_column
column :email
column :password_digest
column :session_token
column :session_key
column :rank
column :profileColour
actions
end
filter :email
filter :session_token
filter :session_key
filter :rank
filter :profileColour
form do |f|
f.inputs "Admin Details" do
f.input :email
f.input :password_digest
f.input :rank
end
f.actions
end
end
Any advice would be greatly appreciated
Thanks!
Try to use :password instead of :password_digest
form do |f|
f.inputs "Admin Details" do
f.input :email
f.input :password
# f.input :password_confirmation
f.input :rank
end
f.actions
end
Don't forget to permit params
ActiveAdmin.register User do
permit_params :email, :password, :rank
end
When I add a new index page in activeadmin, I write some code like this:
# A default activeadmin page for yard overview
ActiveAdmin.register AdminUser do
permit_params :email, :password, :password_confirmation
index do
column :email
column :current_sign_in_at
column :last_sign_in_at
column :sign_in_count
default_actions
end
filter :email
form do |f|
f.inputs "Admin Details" do
f.input :email
f.input :password
f.input :password_confirmation
end
f.actions
end
end
In fact the activeadmin had create some view and controller, and I want to display some document info in yard server, but the yarddoc ignore this file.
What should I do ?
You can not write documentation because Active Admin is Domain-specific language. Write good comments I do not see any other way.
I am using ActiveAdmin as my administration backend in my rails app. Basically, I have an admin_user and a user model.
When I create a new user from an admin account, I specify an email and a password, that is ok.
Let's say I then want to modify the user's email but not the password... it seems this cannot be done as the password field cannot be blank when updating a user.
Is there a configuration somewhere that would consider that the password is unchanged is the fields (password and password_confirmation) are left blank while updating a user?
You don't really need to mess at all with Devise's registration controller, you can just ignore empty password fields inside ActiveAdmin's resource controller:
ActiveAdmin.register User do
controller do
def update
model = :user
if params[model][:password].blank?
%w(password password_confirmation).each { |p| params[model].delete(p) }
end
super
end
end
end
Devise provides an update_without_password method that you can use when updating a user if no password is entered. Using that method, you can customize the update method in your ActiveAdmin users controller.
def update
#user = User.find(params[:id])
if params[:user][:password].blank?
#user.update_without_password(params[:user])
else
#user.update_attributes(params[:user])
end
if #user.errors.blank?
redirect_to admin_users_path, :notice => "User updated successfully."
else
render :edit
end
end
The Devise Wiki has more information about this method if your interested.
You need to evaluate password and password_confirmation in the if statement, to apply the validations on "password_confirmation", eg for my case:
#app/admin/user.rb
controller do
def update
if params[:user][:password].blank? && params[:user][:password_confirmation].blank?
params[:user].delete("password")
params[:user].delete("password_confirmation")
end
super
end
end
#app/model/user.rb
validates :name, :email, presence: true
validates :password, :password_confirmation, presence: true, on: :create
validates :password, confirmation: true
This allows me to validate password presence only when I create a new user and update without changing his password.
This work for me, I hope this is helpful.
I hope this is helpful.
You can validate the password only on create because bcrypt when updating will still validate password presence.
class User
validate :password, :password_confirmation, presence: true, on: :create
end
In my opinion this is much simpler while causing no risk, and allows you to use a single partial form for create and update routes with an if statement showing/not showing password input field like so:
<%= form_for(user, url: users_path) do |form| %>
<%= form.label 'Name' %><br>
<%= form.text_field :name%><br>
<%= form.label 'email' %><br>
<%= form.text_field :email%><br>
<%= form.label 'Password' %><br>
<%= form.password_field :password%><br>
**<% if form.object.new_record? %>**
<%= form.label 'password_confirmation' %><br>
<%= form.password_field :password_confirmation%><br>
**<% end %>**
<%= form.submit (form.object.new_record? ? 'create' : 'update') %>
As told in comments on the #mauriceomdea answer, the def update is missing (or at least was missing for me, henerating an error.)
Here is a more complete version that worked for me :
You don't really need to mess at all with Devise's registration controller, you can just ignore empty password fields inside ActiveAdmin's resource controller:
ActiveAdmin.register User do
controller do
def update
model = :user
if params[model][:password].blank?
%w(password password_confirmation).each { |p| params[model].delete(p) }
end
super
end
end
end
hope this helps someone.