Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tables for New/Edit Views - Editing multiple child records #1097

Closed
seanlinsley opened this issue Mar 1, 2012 · 15 comments
Closed

Tables for New/Edit Views - Editing multiple child records #1097

seanlinsley opened this issue Mar 1, 2012 · 15 comments

Comments

@seanlinsley
Copy link
Contributor

Hello,

I need to be able to easily view and edit large amounts of data. This is in the from of a child model, with multiple values in each record needing to be set but each may not hold the same value necessarily (think between 5 and 30 child records). Step one in this is making the data viewable in an embedded table in the new/edit form so it takes up less space, with step two allowing in-line editing (AJAX) and batch actions as with #247.

The best way forward for step one would probably be to just render the very same table that's in the index view, but I can't figure out how to. I have a feeling this will screw around with the CSS on the page, but I can fix that when I get there.

@seanlinsley
Copy link
Contributor Author

Curiously, both of these work:

form do |f|
  f.inputs "Recipe Details" do
    f.input :title
    f.input :owner
  end
# # # # 1
  table_for(recipe.ingredients) do
    column :name
    column :availability
  end
# # # # 2
  panel "Ingredients" do
    table_for(recipe.ingredients) do
      column :name
      column :availability
    end
  end
end

Both override the form itself, but each have their own formatting (the panel looks just like its counterpart on the 'show' view and the simple table_for is uniquely plain)

@seanlinsley
Copy link
Contributor Author

I'm looking into just how Formtastic and ActionView render these forms, and I found this to render the form at the VERY top of the page (before the <html> tag!)

## app/admin/recipe.rb
form do |f|
  # ...
  custom_table(recipe.ingredients)
  f.buttons
end

## /app/helpers/recipe_helper.rb
module RecipeHelper
  def custom_table(stuff)
    table_for ingredients, :class => "index_table", :id => "ingredients" do
      column :name
      column :availability
    end
  end
end

This is a step farther since now it shows up on the same page... but I think lib/active_admin/form_builder.rb is going to have to be modified to accept the table into its buffer.

@pcreux
Copy link
Contributor

pcreux commented Mar 7, 2012

I don't think that you can do that with Formtastic. You can create your own partial with Rails form helpers and render it with form :partial => 'my_fancy_form'.

@pcreux pcreux closed this as completed Mar 7, 2012
@seanlinsley
Copy link
Contributor Author

Looking at ActiveAdmin::FormBuilder, it appears that f.has_many itself was a hack onto formtastic by you guys. It shouldn't take much work to refactor has_many into something like table_of.

@seanlinsley
Copy link
Contributor Author

For now I've made this partial that does almost everything I want: https://gist.github.com/2038003
It's still buggy, but it's a huge improvement from the standard AA has_many forms.

@seanlinsley
Copy link
Contributor Author

Here's an illustration of what forms could be :) (using the above gist) A better has_many form

Note: this is a 'new' form. On an 'edit' form, there are buttons on the right to remove child records from the table.

Edit: I figured out how to remove the child records, and the gist has been updated to reflect that. I make a DELETE request via jQuery and also remove that row from the page.

@seanlinsley
Copy link
Contributor Author

I'd like to request that this issue be re-opened. While (as @pcreux said) I can do what I wanted using a form partial, I really shouldn't need to--this should be an option baked into Active Admin.

@drush
Copy link

drush commented Apr 11, 2012

+1

@pcreux
Copy link
Contributor

pcreux commented Apr 12, 2012

@daxter What about opening a pull requests with an enhanced "has_many"? :)

@seanlinsley
Copy link
Contributor Author

I would have already, but that requires "porting" it from a partial to AA itself. Once I've done that I'll be sure to send in a pull request.

@fredriksundstrom
Copy link

+1

@seanlinsley
Copy link
Contributor Author

@pcreux What should I name the feature? Should I just replace the current f.has_many? I imagine it would be best to allow users to use either type of form.

@jperville
Copy link

Hello @daxter

Using https://github.com/nathanvda/cocoon library I was able to manipulate a nested model from within an ActiveAdmin form. With some stylesheet I could make all the inputs of the nested model to appear inlined as in your above gist, but with removal actually working.

Note that I had to escape from ActiveAdmin "form" definition to a partial to output both the form and the link to the "add new" button.

@seanlinsley
Copy link
Contributor Author

Thanks for the tip @jperville, but I figured it out a while ago. I just updated the gist with my solution starting on line 24 (sending a DELETE request to the server via jQuery)

@giorgosdi
Copy link

Hello
I have the exact same problem here.Instead of parent i have time_report and istead of child i have time_sheet.I used your partial and everything worked fine but when i hit the create button only the time_report(parent) gets created.
I guess the problem is in the relationship between the models but i cannot figure it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants