How to submit multiple values for an object property in rails? - ruby-on-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!

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
....
....

filter dropdown appear as object in Active Admin in rails 5

ActiveAdmin.register Type do
permit_params :name, :job, version_ids: []
index do
id_column
column :name
column :job
column :versions do |type|
type.versions.collect(&:name).join(', ')
end
actions
end
form(html: { multipart: true }) do |f|
f.inputs do
f.input :name, as: :string
f.input :job, as: :string
f.input :versions, as: :select, input_html: { multiple: true }
end
f.actions
end
end
This table has a has_many relation with workers table. The dropdown which shows the worker filter shows the worker object instead of the worker name. How can I get the worker name in the dropdown instead of the worker object.
Try:
f.input :versions, as: :select,
collection: versions.pluck(:name, :id),
input_html: { multiple: true }

How to show persisted images in ActiveAdmin?

ActiveAdmin automatically pulls up the persisted information for an object when the update form is displayed. It doesn't show the images though - doesn't even show the name in my form - how do I fix this to show the actual image and it's name?
ActiveAdmin.register Art do
permit_params :art_pic_attachments_attributes: [:id, :picture, :_destroy]
form(html: { multipart: true }) do |f|
f.inputs do
f.has_many :art_pic_attachments, allow_destroy: true, heading: 'Images' do |ff|
ff.file_field :picture, required: true, heading: "Pictures"
end
end
f.actions
end
Maybe this wiki article will help. The key is to use an image tag as the input hint.
f.inputs "Attachment", :multipart => true do
f.input :cover_page, :as => :file, :hint => image_tag(f.object.cover_page.url)
f.input :cover_page_cache, :as => :hidden
end

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.

activeadmin: adding delete for a nested resource

I have a infrastructure object composed for many datacenters. In the apps/admin/infrastructures.rb I have the following code:
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.input :name
end
end
f.buttons
end
I can add datacenters with no problems but I don't know how I can delete it from infrastructure form.
Sep 2017 Update:
Rails 5.1.4, ActiveAdmin 1.0.0
Append :id and _destroy in permit_params along with other attributes from the model e.g. :name in your case. Then provide the :allow_destroy option in f.has_many too. Other requirements remain the same; like adding allow_destroy: true in accepts_nested_attributes_for.
Final look:
ActiveAdmin.register Infrastructure do
permit_params :name, datacenters_attributes: [:id, :_destroy, :name]
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters, heading: false,
allow_destroy: true,
new_record: false do |datacenter_form|
datacenter_form.input :name
end
end
f.buttons
end
end
ActiveAdmin Reference
This worked for me:
i.input :_destroy, as: :boolean
and in the Model remember to add :allow_destroy :
accepts_nested_attributes_for :images, allow_destroy: true
Solved adding the following line:
datacenter_form.input :_destroy, :as => :boolean, :required => false, :label => 'Remove'
The code looks like:
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.input :name
datacenter_form.input :_destroy, :as => :boolean, :required => false, :label => 'Remove'
end
end
f.buttons
end
If you cant destroy the object nested. You need to put :_destroy in your app/admin/object.rb permit_params
permit_params :id,:name, :cod, :_destroy
I hope this will be helpful (I've changed my code to suit your example, so I hope there are no typos here):
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.inputs :datacenters do
datacenter_form.input :name
end
datacenter_form.buttons do
link_to "Delete", admin_datacenter_path(datacenter_form.object), method: "delete", class: "button" unless datacenter_form.object.new_record?
end
end
end
f.buttons
end
and the controller method should be defined in datacenters.rb
controller do
def destroy
#datacenter = Datacenter.find(params[:id])
#datacenter.destroy
redirect_to edit_admin_retailer_path(#datacenter.infrastructure)
end
end
This should work:
datacenter_form.label :_delete
datacenter_form.check_box :_delete
This adds a checkbox for each nested object which will delete the object if checked.
Don't forget to add the following to your parent model
has_many :child_name, :dependent => :destroy

Resources