Activeadmin form - ruby-on-rails

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.

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 bcrypt users password on update or insert 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

How to submit multiple values for an object property in rails?

I think a relatively easy question I've had a tough time with grasping in Rails.
How do I get Rails to accept multiple values for an object's property? And how do I display those items. Currently, when I try to display, it has blank fields after I create the item.
I'm using Active Admin so the permits looks a bit different below but same concept as would be in a controller.
Here's code (the input form and params are below):
ActiveAdmin.register Object do
# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
permit_params :name, :description, :price, :style_number, :design, :picture, :materials, :country_made_in, collection_colour_id: []
#
# or
#
# permit_params do
# permitted = [:permitted, :attributes]
# permitted << :other if params[:action] == 'create' && current_user.admin?
# permitted
# end
form do |f|
f.semantic_errors *f.object.errors.keys
f.input :name
f.input :style_number, required: true
f.input :design, required: true
f.input :materials, required: true
f.input :country_made_in, required: true
f.input :description
f.input :collection_colour_id, multiple: true, required: true, :label => 'Colours', :as => :check_boxes, :collection => CollectionColour.all.collect {|u| [u.colour_name, u.id]}
f.input :collection_colour_id, multiple: true, required: true, :label => 'Colours', :as => :check_boxes, :collection => CollectionColour.all.collect {|u| [u.colour_name, u.id]}
f.inputs "Upload" do
f.input :picture, required: true, as: :file
end
f.input :price
f.actions
end
end
The collection_colour_id collects from a collection_colour model. Currently when I create a new object, doesn't accept any values for collection_colour. Appreciate any help!

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

Make a fields_for block conditional

I have a User Model and an Instructor Model. There is a one-to-one relationship between user and instructor. And some users will be instructors and some will not. As such I have a registration form that uses a fields_for method to write to both.
How can I write to the instructor table only on the condition that they say they are an instructor, such as through a checkbox. And when they do write I want to maintain my validations of the table along with the rest of the form
Ideally this would work best if I can do this through the model, but I'm open to all suggestions.
Instructor Model
class Instructor < ActiveRecord::Base
belongs_to :user
validates_presence_of :school_url, :etc...
attr_accessible :school_url, :etc...
end
User Model
class User < ActiveRecord::Base
has_one :instructor, :dependent => :destroy
validates_uniqueness_of :email
validates :email, :confirmation => true
accepts_nested_attributes_for :instructor
attr_accessible :email, :password, :instructor_attributes, :etc
end
Form in HAML
- resource.build_instructor
- form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
= hidden_field_tag :destination, { :value => destination}
.field
= f.label :firstname, "First Name"
= f.text_field :firstname
.field
= f.label :lastname, "Last Name"
= f.text_field :lastname
.field
= f.label :email, "E-Mail"
= f.email_field :email
.field
= f.label :email_confirmation, "Confirm E-Mail"
= f.email_field :email_confirmation
.field
= f.label :password
= f.password_field :password
.field
= f.label :password_confirmation, "Confirm Password"
= f.password_field :password_confirmation
#instructor-box
%p
%span.bold Are you an instructor?
= check_box_tag :instructor_check
%span Yes, I am an instructor
= f.fields_for :instructor do |i|
= render "/users/registrations/instructor", :form => i
I fixed the problem. It seems all too obvious now. In order for the fields_for to be cancelled out all I have to do is delete the instructor_attributes that are being created by the form in the controller. For example:
Create
# params[:user] => {:email => "justin#example.edu", ..., :instructor_attributes => { :school_url => "www.example.edu", ...}
# params[:instructor_check] => "0"
Given these parameters being passed, I can easily delete the attributes that are to be saved and the rails no longer tries to create a new record for instructor that's to be associated with user. This is literally the code I used. Not the most elegant, but it works.
params[:user].delete :instructor_attributes if params[:instructor_check] = "0"
This recognizes that no instructor profile is being created for the user and thus does not write to the table. Before it was sending back blank attributes and failing on the validations.

Resources