ActiveAdmin with form of association opened in default - ruby-on-rails

How to turn the form auto opened without the needs of click in button?
From this button:
https://i.stack.imgur.com/zXOec.png
To this form:
https://i.stack.imgur.com/IRpAD.png
My code is like this:
form do |f|
f.inputs "User" do
f.input :name
f.has_many :addresses, allow_destroy: true do |b|
b.input :street
b.input :number
b.input :complement
end
end
end

Related

How to move this text_method or label_method code from model to view

I'm currently doing this
class User < ApplicationRecord
has_many :courses, dependent: :destroy
end
class Course < ApplicationRecord
belongs_to :user
def title_with_username
"#{title} by #{user.username}"
end
end
<%= simple_form_for #tag do |f| %>
<%= f.input :category_id, collection: Category.all %>
<%= f.input :course_id, collection: Course.all, label_method: :title_with_username, value_method: :id %>
<%= f.button :submit %>
<% end %>
Is it possible to do the label_method all in the view without touching the model?
You can convert your existing select tag to something like
<%= f.input :course_id, collection: Course.all.map {|c| [c.id, "#{c.title} by #{c.user.username}"]} %>
You can build this collection array in controller and pass here as views should not be unnecessarily cluttered.
I don't fully understand why but this seems to work
<%= f.input :course_id, collection: Course.all, label_method: lambda { |course| "#{course.title} by #{course.user.username}" }, value_method: :id %>

Nested Form does not Display form for ActiveAdmin

I am current using activeadmin to create a nested form. I can click on the add nested resource button which will display the inputs for the respective nested resources. However, I would like to have the inputs display by default without having to click on the add resource button (i.e the inputs should be displayed once the form is loaded).
I have rechecked my code against the ActiveAdmin docs and also checked various stackoverflow posts but was unable to make much progress.
Picture of how the form current looks like
My code is as follows in admin/project.rb:
form title: 'Create a New Project' do |f|
f.inputs 'Basic Information' do
f.input :category
f.input :name
f.input :location
f.input :size, sortable: :size do |project|
"#{project.size}m2"
end
f.input :published?
f.has_many :project_main_image, heading: 'Project main image', allow_destroy: true, new_record: false do |a|
a.input :photo, hint: image_tag(a.object.photo.thumb.url, class: 'active-admin-thumbnail')
a.input :orientation
end
f.has_many :project_descriptions, allow_destroy: true do |a|
a.input :contents, as: :ckeditor, input_html: { ckeditor: { toolbar: 'Full' } }
end
f.has_many :project_gallery_images, allow_destroy: true do |a|
a.input :photo, hint: image_tag(a.object.photo.thumb.url, class: 'active-admin-thumbnail')
a.input :orientation
a.input :row_order
end
f.actions
end
end
Any feedback or advice. Do let me know if you require more information.
That's what something i did.
you can take nested attributes like this with for keyword:
form do |f|
f.inputs do
f.input :user, input_html: { disabled: true }
f.input :name
f.input :address
f.input :city
f.input :country, as: :string
end
f.buttons
f.inputs "Account Information", for: [:account, f.object.account] do |s|
s.input :active, as: :boolean
s.input :subscription, as: :boolean
s.input :expires_on, as: :datepicker
s.actions
end
end
controller do
def permitted_params
params.permit!
end
end
end
Hope this is helpful.

simple_nested_form deep nesting issue with link_to_add

I have an issue with simple_nested_form which use deep nesting (nesting form_field within form_field), everything works fine but while adding field by "link_to_add" it just add parent's fields not added nested fields.
Following are my has_through relationship.
customer.rb
has_many :users_customers
has_many :users, :through => :users_customers
accepts_nested_attributes_for :users_customers, allow_destroy: true
users_customer.rb
belongs_to :customer
belongs_to :user
accepts_nested_attributes_for :user
user.rb
has_many :users_customers, foreign_key: "user_id"
has_many :customers, :through => :users_customers, :dependent => :destroy
Following is my view file code: _form.html.erb under customer view.
<%= simple_nested_form_for #customer, :validate => true,:html => { class: 'form-horizontal well-white customer-detail', :multipart => 'true' } do |f| %>
<%= f.fields_for :users_customers do |user_cust| %>
<%= user_cust.fields_for :user do |u| %>
<%= u.input :first_name,as: :string, label: false, placeholder: 'Enter First Name' %>
<%= u.input :last_name,as: :string, label: false, placeholder: 'Enter Last Name' %>
<% end %>
<%= user_cust.input :role_id, collection: Role.pluck(:name,:id).reject{|t| t[1] == 4 }, label: false, placeholder: 'Select Role' %>
<% end %>
<%= f.link_to_add "Add User", :users_customers, class: "btn btn-info top-margin"%>
<% end %>
Now while click on 'add user' it just add roles dropdown not adding first_name and last_name text box.
Can anyone help me why this happen? or let me know any solution.
Thanks in advance

How can I use Active Admin's form block to update a table that tracks when an attribute in a resource is updated?

How can I use Active Admin's form block to update a table that tracks when a particular attribute in the current resource is updated?
For instance, I have something like this:
form do |f|
f.inputs "Details" do
f.input :name
f.input :address
f.input :zip
f.input :city
f.input :active_state, as: :select, collection: active_states
f.input :approval_state, as: :select, collection: approval_states
f.input :new_comment
end
f.buttons
end
where everything but new_comment is an attribute that exists on the current resource.
I tried doing something like this in the class for the resource:
def new_comment
history = History.new(:id, Time.now)
history.comment
end
I also tried creating a controller instance variable when that failed, but that didn't work either. The venue class doesn't really have any co-relation to the history class. I'm just trying to create an input column that will create a new row in my History table whenever the user edits a venue and then adds a comment.
The History model looks like this:
class History < ActiveRecord::Base
attr_accessible :comment, :venue_id, :created_at
belongs_to :venue
def initialize(id, datetime)
#id = id
#datetime = datetime
end
end
The form is for a Venue that looks like this:
class Venue < ActiveRecord::Base
attr_accessible :name, :address, :zip, :city
has_many :histories
accepts_nested_attributes_for :histories
//with a bunch of methods
end
I tried doing this after some digging but it didn't work either:
form do |f|
f.inputs "Details" do
f.input :name
f.input :address
f.input :zip
f.input :city
f.input :active_state, as: :select, collection: active_states
f.input :approval_state, as: :select, collection: approval_states
end
f.inputs "History" do
f.has_many :histories do |h|
h.input :id
h.input :comment
h.input :modified_at
end
end
f.buttons
end
In addition to setting up the model relationships and accepts_nested_attributes_for, try this in your ActiveAdmin resource:
form do |f|
f.inputs "Details" do
f.input :name
f.input :address
f.input :zip
f.input :city
f.input :active_state, as: :select, collection: active_states
f.input :approval_state, as: :select, collection: approval_states
f.has_many :histories do |h|
h.input :id
h.input :comment
h.input :modified_at
end
end
f.actions
end

upload multiple images at a time with paperclip using activeadmin

i'm trying to allow myself to upload 5 images for each model using active admin but i can't seem to figure out how to do it. here is my activeadmin code so far:
ActiveAdmin.register Piece do
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Details" do
f.input :name
f.input :description
f.input :cost
f.input :category
f.input :photo, :as => :file, :hint => f.template.image_tag(f.object.photo.url(:medium))
end
f.buttons
end
index do
column :id
column :name
column :cost
column :category
column :inventory_count
column :available_count
column :materials
column :created_at
default_actions
end
end
how would I allow for 5 at a time instead of just 1?
Here's how it'd work:
class Piece
has_many :pictures
accepts_nested_attributes_for :pictures
end
class Picture
has_attached_file :photo
end
Then in your active admin form, you'd
f.has_many :pictures do |ff|
ff.input :photo, as: :file, :hint => ff.template.image_tag(ff.object.photo.thumb.url)
ff.input :_destroy, as: :boolean
end

Resources