I originally posted this up as an issue on nested_form but not had any insight. The detail is here: https://github.com/ryanb/nested_form/issues/66
Recap:
开发者_Go百科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?
Since then I've been tinkering with building my own helpers that will persist the form back whenever I want to add a leg, and makes use of acts_as_list insert_at_position methods. This feels very hacky and not very elegant though. Is there anything out there that might make this easier or another more graceful approach. In essence I want to be able to:
- Add legs in the list between any existing legs or at the end of the list
- To ideally not have to save the objects to the DB until the user saves the form
- And of course, to still be able to hook in the sortable stuff and other goodness that acts_as_list provides.
Suggestions?
精彩评论