ActiveAdmin form on show page without a panel - ruby-on-rails

I'm trying to add a form to the show page of a resource. The form works just fine, but it will only show up if I wrap it in a panel declaration, which results in a panel-within-a-panel.
Any idea how to get the form to show up without wrapping it in a panel?
Here's the code I have right now:
show do |ad|
...
panel "Foobar" do
semantic_form_for [:admin, resource, resource.reactions.build], builder: ActiveAdmin::FormBuilder do |f|
f.inputs "New Reaction" do
f.input :title
f.input :content
end
f.actions
end
end
end
Thanks!

An alternative to the panel would be to use a div.
The root issue is mixing Arbre and non-Arbre components; here those components are panel and the semantic_form_for. This alteration works because the last return value in an Arbre block is wrapped in a text_node when it is a String. In this case, the form is output as a string and then is wrapped as a text_node within the div, which renders the form. See Arbre's Element source for more info.
show do |ad|
div do # <- Note the div
semantic_form_for [:admin, resource, resource.reactions.build], builder: ActiveAdmin::FormBuilder do |f|
f.inputs "New Reaction" do
f.input :title
f.input :content
end
f.actions
end
end
end

Related

Managing the behavior of the "Cancel" link in Rails' ActiveAdmin forms

A typical form produced by the ActiveAdmin DSL might look like so:
ActiveAdmin.register Post do
...
form do |f|
f.inputs do
f.input :title
f.input :content
end
f.actions
end
...
end
The f.actions bit renders both a submit button and a cancel link. The problem is that the latter's target is always the index page. Instead, for a previously persisted record I would like to be taken back to the show page. As a possible workaround, f.actions could be replaced with:
f.actions do
f.action :submit
if resource.persisted?
f.cancel_link({action: :show})
else
f.cancel_link
end
end
This, however, seems verbose and clunky. Am I missing some more idiomatic trick to accomplish the same?

CKEditor on custom page in activeadmin

i got the following issue:
I want to add a form on a custom page (no model) to design an email for mailjet. I already got a custom page and a form with an CKEditor input and an input for the subject. But the subject field isn't shown on the page.
This is my code:
ActiveAdmin.register_page "Mail", namespace: :lku do
def send_mail
end
content do
panel 'Write mail' do
semantic_form_for :mail, :url => "lku/send_mail", method: :post do |f|
f.inputs do
f.input :subject, :input_html => { :name => 'subject' }
end
f.inputs do
f.input :text, as: :ckeditor, :input_html => { :name => 'text' }
end
end
end
end
end
And this is the result:
When I add
f.actions
it looks like this
can someone help me please?
I was able to reproduce the problem without having ckeditor. Only the last element of form would display.
I don't know exactly what's going on, but it has something to do with how Arbre renders the content you generate inside the content block.
My solution is this
Transform the form content into .erb and move the form under views/lku/mail/_mailform.html.erb
<%= semantic_form_for :mail do |f| %>
<%= f.inputs do %>
<%= f.input :subject %>
<%= f.input :text %>
<% end %>
<%= f.actions %>
<% end %>
Include the form in the page
content do
panel 'Write mail' do
render partial: 'mailform'
end
end
See if you can still mount the editor by using regular Rails form helpers - https://github.com/galetahub/ckeditor#form-helpers

active admin inputs block inside has_many in nested form?

In Active Admin I want to add image upload on an association on a model inside a nested form, using the method shown here. The code doesn't cause any error, but when I load the form, the file upload section (the part using inputs inside the has_many), doesn't show up at all. Code looks something like this:
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs “My Model” do
f.has_many :model_associations do |ma|
ma.inputs “Image Upload” do |image|
image.input :file, as: :file
end
end
end
end
I wonder if the problem is that I have an inputs inside the has_many? Should I be able to have an inputs inside a has_many?
I think that you don't need this line at all:
ma.inputs 'Image Upload' do |image|
Just rewrite it like so:
form do |f|
f.semantic_errors(*f.object.errors.keys)
f.inputs 'My Model' do
f.has_many :model_associations do |i|
i.input :file, as: :file
end
end
f.actions
end

Add custom button to active admin form

Is it at all possible to add a custom button along side the submit and cancel button that is generated with f.actions in this case?
The documents state:
form do |f|
f.semantic_errors # shows errors on :base
f.inputs # builds an input field for every attribute
f.actions # adds the 'Submit' and 'Cancel' buttons
link_to 'Preview', preview_my_admin_panel_posts_path(#post)
end
How could I add something here?
Update
I have got my custom button to show now with this:
inputs 'Submit' do
f.actions do
f.action :submit
f.action :cancel
f.action :reset
li do
link_to 'Preview', preview_my_admin_panel_posts_path()
end
end
Now I can't seem to grab the form object and pass through the params for post, title and comments.
Yea, it is possible.
define an action_item:
action_item only: %i(new edit) do
link_to 'Preview', preview_my_admin_panel_posts_path(#post)
end
Ok, I think you could do the following:
form do |f|
f.semantic_errors
f.inputs
f.actions do
f.submit, as: :button, label: 'Optional custom label'
f.cancel, as: :link # I think could converted to button as submit
link_to 'Preview', preview_my_admin_panel_posts_path(#post)
end
end

Active Admin: Customize only new form

I'm using Active Admin to provide an admin to some models. I need to provide a customized new form for one of them, but leave the edit form as the default provided by Active Admin. Here's what I have. It works in that it is giving me the new form I want, but the edit form is also using the new form, which is not what I want:
ActiveAdmin.register Document do
form :partial => 'form'
end
I've tried this, but it gives an error that 'new' is an undefined method:
ActiveAdmin.register Document do
new do
form :partial => 'form'
end
end
If you just want to hide or show certain fields on the new form (e.g. a field that you generate automatically in the model using before_create), you can do this:
form do |f|
f.inputs "Member Details" do
f.input :first_name
f.input :last_name
f.input :email
if !f.object.new_record?
f.input :password
f.input :password_confirmation
end
end
f.button :Submit
end
This will hide the password fields when you create a new member in the event that you automatically generate passwords the first time the member is created.
I've figured out a way to do it with some logic in the view. Not the best way, to be sure, but it does what I want until I figure out a better way. Here's the logic I'm using:
<% if controller.action_name == 'new' %>
new form
<% else %>
edit form
<% end -%>
I am not sure it can be done directly with form. If you take a look at the code you'll see that only the last call is taken into account. On the other hand, you may try something like:
config.set_page_config :new do
form :partial => 'form'
end
But I would rather ask the developers for this feature.
If someone wants to render different partials for new and edit pages you have to:
#app/admin/document.rb
ActiveAdmin.register Document do
form partial: 'form'
end
#app/views/admin/documents/_form.html.erb
<% if #document.new_record? %>
<%= render partial: "form_new", resource: #document %>
<% else %>
<%= render partial: "form_edit", resource: #document %>
<% end %>
#app/views/admin/documents/_form_new.html.erb
<%= semantic_form_for [:admin, #document], builder: Formtastic::FormBuilder do |f| %>
<%= f.semantic_errors %>
<%= f.inputs do %>
<%= f.input :name %>
<% end %>
<%= f.actions %>
<% end %>
You could create a custom page that acts as the new form, render a partial there which contains arbitrary form code.
So, in your admin directory you make a file new_document.rb containing
ActiveAdmin.register_page "New Document" do
content do
panel "Create a new document" do
render :partial => "admin/documents/custom_form", :locals => {document: Document.new}
end
end
end
You then put your arbitrary formtastic form in admin/documents/custom_form and your arbitrary controller action aka collection_action in admin/documents.
So basically doing normal rails type stuff within the activeadmin framework.

Resources