ActiveAdmin bcrypt users password on update or insert rails - ruby-on-rails

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

Related

The form in the active admin gives me the option of inserting an empty value

I have this enum in the user model
enum role: { Principal: 0, Teacher: 1 }
In the active admin, in the create user form, the input to insert the role gives me the option of inserting an empty string.
How can I make it that it only gives me the options Principal and Teacher?
ActiveAdmin.register User do
actions :all, except: [:edit]
permit_params :email, :name, :password, :password_confirmation, :role
form do |f|
f.inputs do
f.input :email
f.input :name
f.input :password
f.input :password_confirmation
f.input :role
end
f.actions
end
end
For this you can pass a include_blank: false option
Means according to your code :-
....
....
f.input :role, include_blank: false
....
....

Activeadmin form

Just want to ask if there's way to add a text field on activeadmin for example on User model. This text field is not an attribute of my User model.
app/admin/user.rb
ActiveAdmin.register User do
# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
permit_params :email, :password, :password_confirmation, :TierID, :RoleID, :FirstName,
:MiddleName, :LastName, :MobileNumber, :Company, :Gender, :Birthdate,
:FacebookID, :TwitterID, :WorkingStatus, :Occupation, :CreatedBy, :MerchantID
#
# or
#
# permit_params do
# permitted = [:permitted, :attributes]
# permitted << :other if resource.something?
# permitted
# end
index do
selectable_column
id_column
column :email
column :RoleID
column :TierID
column :FirstName
column :MiddleName
column :LastName
column :MobileNumber
actions
end
before_create do |user|
user.CreatedBy = current_admin_user.email
end
after_create do |user|
roleid = Role.find_by(Description: "merchant").RoleID
roleuser = User.last
if roleuser.RoleID == roleid then
begin
mlastid = Merchant.last.id
ulastid = User.last.id
merchantid = mlastid + 1
rescue
merchantid = 1
end
m = Merchant.new
m.MerchantID = merchantid
m.UserID = ulastid
m.save
else
roleuser.TierID = 1
roleuser.update(id: roleuser.id)
end
end
form do |f|
f.inputs "User Details" do
#role = Role.find(1)
role = Role.all
f.input :email
f.input :password
f.input :password_confirmation
#options_for_select(#user.map{ |m| [m.FirstName, m.id]}), :include_blank => true %>
f.input :RoleID, as: :select, :collection => role.map { |r| [r.Code, r.id]}
f.input :FirstName
f.input :MiddleName
f.input :LastName
f.input :MobileNumber
f.input :Company
f.input :Gender
f.input :Birthdate, :start_year => Time.now.year - 100, :end_year => Time.now.year
f.input :FacebookID
f.input :TwitterID
f.input :WorkingStatus
f.input :Occupation
end
f.actions
end
end
Additional to my problem, the added text field is an attribute of other model. Thanks. Cheers!
Firstly if you want to add a field which is not there inside your model then you can use attr_accessor in your user model.
attr_accessor :field_name
Then you can use this field inside your active admin views.
Also, you mentioned at the end that the added text field is an attribute of other model - so in this case do you want to updated a column of your associated model which you can achieve using accept_nested_attributes.

Active Admin remove certain fields while going for updation

Hi i have an admin page using active admin gem.
thing is while creating a new page i should be able to input name, amount and interval..But while updating only name field must show..other 2 values shouldnt be updated. This is my active admin file. How to make this happen. Thanks in advance
ActiveAdmin.register SubscriptionPlan do
menu priority: 10
permit_params :name, :amount, :interval
index do
selectable_column
default_actions
column :name
column :amount
column :interval
end
form do |f|
f.inputs "Subscription Plan" do
f.input :name
f.input :amount
f.input :interval, as: :select, collection:["week","month","year"]
end
f.actions
end
end
Try this
form do |f|
f.inputs "Subscription Plan" do
f.input :name
if f.object.new_record?
f.input :amount
f.input :interval, as: :select, collection:["week","month","year"]
end
end
f.actions
end

How can I use yard doc in ActiveAdmin?

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.

Adding New Admins to Active Admin

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")

Resources