nested_form gem的麻烦

时间:2013-02-07 17:49:22

标签: ruby-on-rails ruby-on-rails-3 nested-forms

我正在设置nested_form gem,如下所示:https://github.com/ryanb/nested_form

问题是我的f.fields_for似乎空了。我没有任何错误,但在页面上我只看到“添加位置”链接,它什么都不做。

数据模型是Events => event_locations< = Locations,event_locations充当联结对象。

以下是我认为的表格:

 <%= nested_form_for @event do |f| %>
    <%= @event.locations %>
    <%= f.fields_for :locations do |task_form| %>
      <%= task_form.text_field :name %>
      <%= task_form.link_to_remove "Remove this Location" %>
    <% end %>

    <p><%= f.link_to_add "Add a Location", :locations %></p>
  <% end %>

这是我的事件控制器:

def new
    logger.debug "*** before Event.new"
    @event = Event.new
    logger.debug "*** after Event.new"
    3.times do
      @event.locations.build
    end   
  end

event.rb

has_many :days_events
  has_many :events_locations
  has_many :locations, :through => :events_locations

  # Validations
  validates_presence_of :name

  accepts_nested_attributes_for :locations

event_location.rb

belongs_to :event
belongs_to :location

accepts_nested_attributes_for :location

location.rb

has_many :events_locations
has_many :events, :through => :events_locations


#validations
validates_presence_of :name

accepts_nested_attributes_for :events

1 个答案:

答案 0 :(得分:0)

nested_forms gem有点旧。我建议在他的屏幕演员(http://railscasts.com/episodes/196-nested-model-form-revised)的修订剧集中使用ryan的代码。我认为您需要专业帐户才能看到它,但这是值得的。

或者,按照原始屏幕投射(http://railscasts.com/episodes/197-nested-model-form-part-2)进行修改并免费使用,然后使用该代码。这样你至少知道发生了什么。