1

I'm looking for some guidance to make a custom form with ActiveAdmin. This is not a regular form, but I actually need some JavaScript on it. However, I'm not familiar with ActiveAdmin right now.

I have a form that will collect a Product list. Every time I add a Product to the list, I need to recalculate the sub-total for the order (based on quantity and unique price).

For adding the products I'm using regular Formtastic, like this:

f.inputs "Product List" do
  f.has_many :product_lists do |detail|
    detail.input  :good_id, :as => :select,
                  :collection => Good.accessible_by(current_ability, :read),
                  :input_html => { class: 'chosen-select' },
                  :include_blank => true
    detail.input :quantity, :input_html => { :value => 1 }
  end
end

However, I came across to multiple questions:

  • How should I recalculate the sub-totals every time I add a new product on the list? Should I use a custom collection action? I was even considering a Backbone App inside of it, for handling the whole process.
  • Is there a better way, instead of using a Custom Action?
  • Is there a good way to use a custom action ONLY for the new form? I was able to make a new one, but I was not able to have control over the form.
  • How can I have better control of the form panel? I was not able to add panels inside the form block :(.
demogar
  • 701
  • 1
  • 8
  • 17
  • If you have multiple different questions - then it's much better if you ask them as separate questions on Stack overflow because people will then know whether or not all of your questions have been answered. Please split them up and ask them separately – Taryn East Dec 17 '13 at 05:25

1 Answers1

1

If you are using >= 1.0.0.pre from the master branch:

  • you should be able to wrap a has_many block inside of an f.inputs block if you want them to be in a panel.
  • there are callbacks for before/after add/remove for has-many forms. You might be able to hook into those for recalculating (at least on removals). For additions, you might want to consider adding a change listener for whatever should trigger recalculation. You can add that listener on page load and/or after a new item is added.

Is your total being recalculated on the server side or on the client side when you add items? If on the server side, the total should be updated after the form is submitted, and that logic probably belongs in your model. AA has-many adds fields to the form, which in turn gets submitted and then committed. Adding a new nested fieldset does not change anything on the server until the whole form is submitted. If it should be updated without committing, you'll need to handle the ajax request and responses yourself, but you should be able to simply use the default actions and either request the json format back or create a custom javascript template.

Josh Kovach
  • 7,679
  • 3
  • 45
  • 62
  • Hi Josh, thanks for your answer. If I'm trying to recalculate it without committing, via JavaScript, how can I calculate the totals every time an item is sent? I mean, is there any special listener for when an item is added (JavaScript level) or how? – demogar Dec 18 '13 at 20:41
  • 1
    It depends on how you're setting the value that needs to be added to the sum. You can typically add a `change` listener to whatever input would have that value, and then run through and sum them all up again. – Josh Kovach Dec 18 '13 at 21:37
  • Hi Josh. Thanks again. That sounds reasonable. Correct me the flow if wrong: I will use the code I provided before (so that I can use formtastic/AA for adding the items). Then, I need to register a listener on the container, so that every time I add an item, I will trigger an Ajax call, look up for the price for that item and quantity on the form, then calculate the totals via JavaScript. – demogar Dec 18 '13 at 22:36
  • 1
    You should listen for ActiveAdmin's `has_many_add:after` callback and add your change listener to your select field. The change listener for the select field should then make the ajax call to look up the price of the selected item, then recalculate the total after a successful response. Bonus points if you can add the price as a data-attribute on the select field (not sure how to do this, and it might be more complicated than doing the ajax call, but would be quicker and more responsive if you can figure it out). – Josh Kovach Dec 19 '13 at 14:24
  • 1
    RE: Adding data attributes to select options: http://stackoverflow.com/a/6374301/447934 – Josh Kovach Dec 19 '13 at 15:03
  • Ah, didn't know about the `has_many_add:after` callback. Will give it a try and let you know how it goes. Thanks a million Josh! Will come back to mark the question as answered if I got no more questions. – demogar Dec 19 '13 at 15:03
  • It's pretty new. Only added a few weeks ago. And also only available on master branch >= 1.0.0.pre, so make sure you're using that one. – Josh Kovach Dec 19 '13 at 15:11
  • @demogar you could add a response with the final code you found out, in order to help the community struggling with this ;) – Pak Jun 06 '16 at 08:13