Add custom button to active admin form - ruby-on-rails

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

Related

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

ActiveAdmin form on show page without a panel

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

Add a custom action button in active_admin input form

Iam working on active_admin form. my requirement is to add a button beside a input box to create new project
How to add a action item inside a form ?
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs "Details" do
f.input :task
f.input :project
f.input :members, :input_html => { :class => "chosen-input" }, :label => 'Assigned To'
f.actions
end
end
i want to add a new project button beside input field project
Though it is a very old question, stil i was also looking for the answer, This is how to do it:
just add it after f.input :project:
f.button "New Project", formaction: "new_entry", class: "some_class"
Then using member_action:
member_action :new_entry, method: :patch do
end
inputs 'Details' do
li do
label "Project"
span do
"here is your project input field"
end
span do
"here is your button"
end
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.

2 submit buttons in a form

I have a question about forms. I have a fairly standard form that saves a post (called an eReport in my app) with a title and body. The table also has a "published" field, which is boolean. The saved eReport only shows on the public site if this field is set to true, with false being the default.
Rather than the default check box, I would like to display two buttons at the end of the form: a "Publish Now" button and a "Save as Draft" button. If the user presses the former, the published field would be set to true. If the latter, then false. In PHP, I used to display 2 submit fields with different name values, then handle the input with an if/else statement to determine the proper SQL query to build. In Rails, I'm assuming I would place this logic in the controller, under the appropriate action, but I'm not sure how to manipulate the name or id values of buttons.
For the record, I'm using Formtastic, but if someone could show me how to do this with the default Rails form tags, that's OK too. Here's the code for my form as it stands right now:
<% semantic_form_for #ereport do |form| %>
<% form.inputs do %>
<%= form.input :title %>
<%= form.input :body %>
<% end %>
<% form.buttons do %>
<%= form.commit_button :label => "Publish Now" %>
<%= form.commit_button :label => "Save as Draft" %>
<% end %>
<% end %>
Thanks in advance for the help!
I don't know about formtastic, but with the default rails form builder, you could do it like this:
<%= form.submit "Save with option A", :name => "save_option_a" %>
<%= form.submit "Save with option B", :name => "save_option_b" %>
Then in the controller, you can pick those up in params:
if params[:save_option_a]
# do stuff
end
in addition to #iddlefingers answer, here is a view of the log of the application (striping some useless params due to explanation purposes)
Parameters: {"utf8"=>"✓", ..., "comentar"=>"Confirmar"}
where we can see that comentar is the name of the parameter, and "Confirmar" is it's value, which is the button's text too.
which was obtained by submit_tag "Confirmar", :name => 'comentar'
So in general you could have (if you want to reduce the number of params you are working with) several submit_tag "onevalue", :name => 'SAMEname', submit_tag "othervalue", :name => 'SAMEname'...
and retrieve them in your controller
if params[:SAMEname] == "onevalue"
# do stuff
elsif params[:SAMEname] == "othervalue"
#do different stuff
end
I think you need to use jQuery.
You can bind the button click event and submit the form for specified location.

Resources