2

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

你能告訴我們的控制器創建行動? – Nerian 2011-01-14 18:29:25

+0

現在已添加! – jonepatr 2011-01-14 18:45:34

回答

2

說實話,我從來沒有嘗試過編輯或創建該方式has_many :through

花了一點時間,不得不修復formtastic_cocoon中的js以使其工作,所以這裏是一個可行的解決方案。

您需要挑選EventUser型號,然後填寫User型號(反之亦然)。

所以模型裏面你寫的:

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部分(其實不一定是偏)

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

這應該有效。 請注意,我必須更新formtastic_cocoon gem,所以你需要更新到0.0.2版本。

現在這將是容易地從一個簡單下拉列表中選擇的user_type,代替隱藏字段,例如使用

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

的幾點思考(現在我證明了它的工作原理):

  • 這將始終在運行中創建新用戶,實際上消除了對EventUser的需要。你會允許從下拉菜單中選擇現有的用戶嗎?
  • 我個人把它周圍:讓用戶自己指定一個事件!
0

是否events_users模型沒有一個ID列?由於有一個額外的字段(user_type),所以EventUser是一個模型,應該有一個ID。也許這就是爲什麼user_type沒有在你的第一種情況下被設置。

+0

我一直在試圖用增加一個ID,但它並沒有任何區別:/ – jonepatr 2011-01-14 18:41:44