我试图让它工作,但它dosen't!Rails 3,嵌套的多级表单和has_many通过
我
class User < ActiveRecord::Base
has_many :events, :through => :event_users
has_many :event_users
accepts_nested_attributes_for :event_users
end
class Event < ActiveRecord::Base
has_many :event_users
has_many :users, :through => :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
这是我的形式
<%= semantic_form_for @event do |f| %>
<%= f.semantic_fields_for :users, f.object.users do |f1| %>
<%= f1.text_field :name, "Name" %>
<%= f1.semantic_fields_for :event_users do |f2| %>
<%= f2.hidden_field :user_type, :value => 'participating' %>
<% end %>
<% end %>
<%= link_to_add_association 'add task', f, :users %>
<% end %>
的问题是,如果我创建一个新的用户这样,它不会设置user_type的值(但它会创建一个user_id和一个event_users,并且具有user_id和event_id)。如果我在创建用户并提交后返回编辑表单,则user_type的值将在events_users中设置。 (我也试过没有formtastic) 有什么建议吗?谢谢!
---- ----编辑
我也试图让用户
<%= semantic_form_for @event do |f| %>
<%= f.semantic_fields_for :event_users do |f1| %>
<%= f1.hidden_field :user_type, :value => 'participating' %>
<%= f1.semantic_fields_for :users do |f2| %>
<%= f2.text_field :name, "Name" %>
<% end %>
<% end %>
<%= link_to_add_association 'add task', f, :event_users %>
<% end %>
前event_users但当时它只是抛出了我一个错误:
User(#2366531740) expected, got ActiveSupport::HashWithIndifferentAccess(#2164210940)
- 编辑 -
的link_to_association是formtastic茧方法(https://github.com/nathanvda/formtast IC-茧),但我试图做其他的方法,但结果相同
---编辑----
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
你能告诉我们的控制器创建行动? – Nerian 2011-01-14 18:29:25
现在已添加! – jonepatr 2011-01-14 18:45:34