简体   繁体   中英

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM