我試圖讓它工作,但它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