开发者

Using acts_as_list and nested_form together in Rails 3.1.rc4 app

开发者 https://www.devze.com 2023-03-24 07:19 出处:网络
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

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:

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?

开发者_Go百科

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:

  1. Add legs in the list between any existing legs or at the end of the list
  2. To ideally not have to save the objects to the DB until the user saves the form
  3. And of course, to still be able to hook in the sortable stuff and other goodness that acts_as_list provides.

Suggestions?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号