Rails 3,嵌套的多级表单和has_many通过

我试图让它工作,但它不是!

我有

class User  :event_users has_many :event_users accepts_nested_attributes_for :event_users end class Event  :event_users accepts_nested_attributes_for :users end class EventUser < ActiveRecord::Base set_table_name :events_users belongs_to :event belongs_to :user accepts_nested_attributes_for :events accepts_nested_attributes_for :users end 

还有表格布局

 event_users user_id event_id user_type events id name users id name 

这是我的forms

      'participating' %>     

问题是如果我以这种方式创建一个新用户,它不会设置user_type的值(但它会创建一个用户和一个带有user_id和event_id的event_users)。 如果我在创建用户并提交后返回编辑表单,则在events_users中设置user_type的值。 (我也尝试过没有formtastic)任何建议? 谢谢!

– – 编辑 – –

我也尝试过在用户之前使用event_users

    'participating' %>       

但那只会给我一个错误:

用户(#2366531740)预计,获得ActiveSupport :: HashWithIndifferentAccess(#2164210940)

– 编辑 –

link_to_association是一种formtastic-cocoon方法(https://github.com/nathanvda/formtastic-cocoon),但我尝试过其他方法,但结果相同

– -编辑 – –

 def create @event = Event.new(params[:event]) respond_to do |format| if @event.save format.html { redirect_to(@event, :notice => 'Event was successfully created.') } format.xml { render :xml => @event, :status => :created, :location => @event } else format.html { render :action => "new" } format.xml { render :xml => @event.errors, :status => :unprocessable_entity } end end end 

说实话,我从未试图编辑或创建一个has_many :through这种方式。

花了一点时间,并且不得不修复formtastic_cocoon中的js以使其工作,所以这是一个有效的解决方案。

您需要调整EventUser模型,然后填充User模型( EventUser将永远不会工作)。

所以在你写的模型中:

 class Event < ActiveRecord::Base has_many :event_users has_many :users, :through => :event_users accepts_nested_attributes_for :users, :reject_if => proc {|attributes| attributes[:name].blank? }, :allow_destroy => true accepts_nested_attributes_for :event_users, :reject_if => proc {|attributes| attributes[:user_attributes][:name].blank? }, :allow_destroy => true end class EventUser < ActiveRecord::Base belongs_to :user belongs_to :event accepts_nested_attributes_for :user end class User < ActiveRecord::Base has_many :events, :through => :event_users has_many :event_users end 

然后是意见。 从events/_form.html.haml

 = semantic_form_for @event do |f| - f.inputs do = f.input :name %h3 Users (with user-type) #users_with_usertype = f.semantic_fields_for :event_users do |event_user| = render 'event_user_fields', :f => event_user .links = link_to_add_association 'add user with usertype', f, :event_users .actions = f.submit 'Save' 

(我现在忽略错误)然后,你需要指定部分_event_user_fields.html.haml部分(这里有点神奇):

 .nested-fields = f.inputs do = f.input :user_type, :as => :hidden, :value => 'participating' - if f.object.new_record? - f.object.build_user = f.fields_for(:user, f.object.user, :child_index => "new_user") do |builder| = render("user_fields", :f => builder, :dynamic => true) 

并结束_user_fields partial(这不一定是部分)

 .nested-fields = f.inputs do = f.input :name 

这应该工作。 请注意,我必须更新formtastic_cocoon gem ,因此您需要更新到版本0.0.2。

现在很容易从简单的下拉列表中选择user_type ,而不是隐藏字段,例如use

 = f.input :user_type, :as => :select, :collection => ["Participator", "Organizer", "Sponsor"] 

一些想法(现在我certificate它有效):

  • 这将始终创建新用户,实际上消除了对EventUser 。 您是否也允许从下拉列表中选择现有用户?
  • 我个人会转过身来:让用户自己参加一个活动!

events_users模型没有ID列吗? 由于还有一个附加字段(user_type),因此EventUser是一个模型,应该有一个ID。 也许这就是为什么在第一种情况下没有设置user_type的原因。