2011-06-20 72 views
0

我試圖創建表單來創建一個事件。活動屬於創建者(類用戶),表單包含fields_for創建者。fields_for不呈現關聯名稱(belongs_to關係)

很大程度上,表單正在工作 - 它正確地呈現其他所有內容,並且當fields_for @ event.creator部分被註釋掉時,create動作正確地處理它。雖然,fields_for標籤渲染事件[用戶]標籤,而不是事件[創建者],我不明白爲什麼。想法?這裏是相關的摘錄。

(在視圖/活動/ new.html.erb)

<%= form_for @event do |form| %> 
    <%= render :partial => '/events/form', :object => form %> 
    <%= form.submit 'Go!' %> 
<% end %> 

(在事件/ _form.html.erb)

... 

<% if !signed_in? %> 
    To create your event, please provide an email address and create a password. <br /> 


    <%= form.fields_for @event.creator do |uf| %> 
    <div class="email_field"> 
     <%= uf.label :email %> 
     <%= uf.text_field :email, :type => "email" %> 
    </div> 

... 
    <% end %> 
<% end %> 

(控制器/ events_controller.rb)

def new 
    @event = Event.new 

    if !signed_in? 
    @event.build_creator 
    end 

    respond_to do |format| 
    format.html # new.html.erb 
    format.xml { render :xml => @event } 
    end 
end 

(型號/ event.rb)

belongs_to :creator, :class_name => "User" 
accepts_nested_attributes_for :creator 

很大程度上這個工作,但窗體元素渲染爲

<div class="email_field"> 
    <label for="event_user_email">Email</label> 
    <input id="event_user_email" name="event[user][email]" size="30" type="email" /> 
</div> 
<div class="password_field"> 
    <label for="event_user_password">Password</label> 
    <input id="event_user_password" name="event[user][password]" size="30" type="password" /> 
</div> 
... 

這裏的問題是event_user和活動[用戶]代替EVENT_CREATOR和活動[創造者];當我提交表單時,我得到一個錯誤,該事件沒有用戶字段。

回答

3
<%= form.fields_for :creator, @event.creator do |uf| %> 
+0

謝謝!事實證明,這是兩件事:我需要這個,我需要修復關聯的用戶端。我有'has_many:事件,'但沒有',:foreign_key =>「creator_id」'。固定。再次感謝... –

相關問題