Activeadmin form use show view - ruby-on-rails

How can i implement such thing as display part of my view page on edit page
I want to show some data under my edit form.
form do |f|
f.semantic_errors(*f.object.errors.keys)
f.inputs 'Edit SSR' do
f.input :scope
f.input :code
f.input :name
end
f.actions
and here my view data goes
.....
end
I have tried to override form but no luck
How i can do it?

If you want to show data below the form then you can write it inside div as follows in the active_admin resource file:
form do |f|
f.semantic_errors(*f.object.errors.keys)
f.inputs 'Edit SSR' do
f.input :scope
f.input :code
f.input :name
end
f.actions
div id: "div_id", class: "div_class" do
#put your data here
end
end
OR you can create a partial and call it as:
form partial: "partial_name"

Related

Active admin Place a form above the table

I have an application using active admin. All I tried to achieve is , to show a form above the active admin table. When the form is submitted , a request is placed which will be displayed in the table below the form. What I tried is,
Tried to render the form in a panel within index
Tried to have 2 index, one with the table and the other with rendering the form
The problem I faced in the above two solutions is , when there are no records in the table , the form is also not shown . When the table has records, the form is displayed
Is there a way so that the form is shown even if there are no records in index table. or, other ways of showing the form above the table. I also tried to write custom index, but could not locate the exact documentation for that
First off, to render a form you normally do:
form html: {:multipart => true} do |f|
f.inputs do
f.input :name
f.input :content
f.input :description
f.input :price
f.input :plan
f.input :image, hint: f.project.image? ? image_tag(project.image.url, height: '100') : content_tag(:span, "Upload JPG/PNG/GIF image")
end
f.actions
end
inside the model. In my case it was projects.rb for project.rb
If you want to render a form via partial, simply do:
# renders app/views/admin/posts/_some_partial.html.erb
render 'some_partial', { post: post }
this will render any form into your admin panel.
If you want to insert an index:
ActiveAdmin.register Task do
permit_params :title, :note, :video, :header, :tag, :project_id
sortable tree: false, sorting_attribute: :tag
index :as => :sortable do
label :title
actions
end
index do
selectable_column
column :header
column :title
column :tag
column :project
actions
end
end

rails activeadmin ask to show a input field if new

I have a form in ActiveAdmin:
form do |f|
f.inputs t('out_payment_details') do
f.input :amount
f.input :status
f.input :project
f.input :approved_user
f.input :request_user
f.input :from_company
f.input :to_company
end
f.actions
end
What I want:
When new/create, request_user is always the current_user, and if edit(not create), then the approved_user is current_user.
How can I have some conditions of default value with sth like "on new form"?
By using if and else condition like
if f.object.new_record?
input field
else
input field
end

ActiveAdmin: Generating drop down list in form for has_many/belongs_to relationship

I have Gallery and Image models with has_many/belongs_to relationship.
My admin_active form for Image looks like this:
form do |f|
f.inputs "Image Details" do
f.input :gallery_id
f.input :file
end
end
Instead or entering number for gallery_id I'd like to chose gallery name from drop-down list. By deafault this form looks like this, but I don't know how to do this manually.
Change your form to the following
form do |f|
f.inputs "Image Details" do
f.input :gallery_id, as: :select, collection: Gallery.select(:name).uniq
f.input :file
end
end
Try this
form do |f|
f.inputs "Image Details" do
f.input :gallery
f.input :file
end
end

Ruby On Rails Active Admin has_many changing text to use a different column

This is similar to my previous question Ruby On Rails Active Admin has_many changing dropdown to use a different column
I figured out how to reassign a f.inputs but how would i go about reassigning the display of the data when viewing an item...
e.g:
Public Git Repo: https://github.com/gorelative/TestApp
Snippet of code i have in my fillups.rb
ActiveAdmin.register Fillup do
form do |f|
f.inputs do
f.input :car, :collection => Car.all.map{ |car| [car.description, car.id] }
f.input :comment
f.input :cost
f.input :mileage
f.input :gallons
f.buttons
end
end
end
Modify the show action
ActiveAdmin.register Fillup do
# ... other stuff
show do
attributes_table do
# add your other rows
row :id
row :car do |fillup|
link_to fillup.car.description, admin_car_path(fillup.car)
end
end
end
end

rails ActiveAdmin nested form has_one accepts_attributes_for formtastic issue

I am using ActiveAdmin and Rails 3.1 -- having problem understanding whether the following is a bug, or if there is some way to do it correctly that I am not understanding. I am trying to use a nested model with a has one relationship, so that I can create a page and fill out it's meta data in 1 step. --
(page has_one meta_data, accepts_nested_attributes_for meta_data)
Example 1)
in this example, when I click new page, meta data section is there but there are no input fields -- also, if I edit the record, it shows up correctly, however the fieldset is duplicated in the second section... and if I remove the f.inputs wrapping semantic_field_for (which would make sense), then it breaks completely and shows nothing in the meta data area...
form do |f|
f.inputs "Page Information" do
f.input :name
f.input :uri
f.input :view
f.input :body, :as => :text
f.input :active
end
f.inputs "Meta Data" do
f.semantic_fields_for :meta_data do |meta_form|
meta_form.inputs :title, :description, :keywords, :name => "Meta Information"
end
end
end
I understand the meta data probably isn't being instantiated, but I am not sure how I am supposed to do that in the form block? (or if I can even do it) -- The only way I am able to get this to work is by doing using a custom form, and building the meta data in the view, which looks like this
2) How I am working around it, but seems hacky
<%= semantic_form_for [:admin, #page] do |f| %>
<% #page.build_meta_data %>
<%= f.inputs :name => "Page Information" do %>
<%= f.input :name %>
<%= f.input :uri %>
<%= f.input :view %>
<%= f.input :body, :as => :text %>
<%= f.input :active %>
<% end %>
<%= f.semantic_fields_for :meta_data do |meta_form| %>
<%= meta_form.inputs :title, :description, :keywords, :name => "Meta Information" %>
<% end %>
<%= f.buttons %>
<% end %>
Thanks in advance for any help or clarification.
(note to moderators I started another thread on this but was not as clear and didn't have the workaround solution I do now yet, so if one of the questions should be deleted please delete the other)
I found a better solution for you. You can use :for option in inputs helper.
f.inputs "Meta Data", for: [:meta_data, f.object.meta_data || MetaData.new] do |meta_form|
meta_form.input :title
meta_form.input :description
meta_form.input :keywords
end
I think this might work too, but I didn't check
f.inputs :title, :desctiption, :keywords,
name: "Meta Data",
for: [:meta_data, f.object.meta_data || MetaData.new]
In rails 4, this is something that works, with a nice design
e.g.,
A customer has one account
model/customer.rb
accepts_nested_attributes_for :account
admin/customer.rb
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
i was having the same problem, i worked in your hack and got it working.
i then moved <% #page.build_meta_data %> to a custom new method like this
controller do
def new
#tenant = Tenant.new
#tenant.build_tenant_configurable
end
end
hope this helps

Resources