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

Already on GitHub? Sign in to your account

Working with nested attributes and acts_as_list #66

Closed
p7r opened this Issue Jul 28, 2011 · 1 comment

Comments

Projects
None yet
2 participants

p7r commented Jul 28, 2011

I have a slightly unusual requirement that means I need to respect acts_as_list positions when adding/removing nested models.

class Journey < ActiveRecord::Base
  has_many :legs, :dependent => :destroy
  accepts_nested_attributes_for :legs, :allow_destroy => true
end

class Leg < ActiveRecord::Base
  belongs_to :journey
  acts_as_list :scope => :journey
end

I don't think this is that odd - the legs of my journey have a destination, and the order of them determines the start and end points.

Of course, this means I can't use the standard helpers of f.link_to_add and f.link_to_remove as these are form level helpers, and I want a helper to sit with the nested form. I can handle the JS to update positions of subsequent legs no problem, but I was wondering if there was an established way of doing this already, or whether I need to fork, build new helpers and then issue a pull request? I was thinking something like being able to do a:

  <%= f.link_to_add_inline "Add leg", :legs, :after => current_leg.position %>

Thoughts?

Collaborator

lest commented Jun 5, 2012

If you are using jQuery, nested:fieldAdded and nested:fieldRemoved events are triggered on the form element after adding and removing fields.

I think that use of these events is the best approach to update positions.

@lest lest closed this Jun 5, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment