2012-05-05 74 views
0

我有一個表單,我正在創建一個新項目。如何從視圖訪問has_many關係?

= form_for(@project, :html => { :class => 'addProjectForm' }) do |f| 
    #actions 
    = f.submit 
    %p 
    = f.label :name 
    %br 
    = f.text_field :name, :class => 'projectName' 
    %p 
    = f.label :ticket 
    %br 
    = f.text_field :ticket, :class => 'ticket' 
%p 
    = f.label "Crew Member Name" 
    = f.label "Crew Member Role" 
    %br 
    = f.text_field :crew_members.name 
    = f.collection_select :crew, CrewMember.all, :role, :role 
    = f.submit "Add More Crew Members", :id => 'add-crew-member' 
    %p 

一切工作正常的點:crew_members.name。我不斷收到undefined method "name" for :crew_member:Symbol。所有以前的標籤都在更新項目表,但我不能讓CrewMembers更新。這是模型。

class CrewMember < ActiveRecord::Base 
    attr_accessible :name, :role 
    belongs_to :project 
end 

感謝您的幫助提前。

+0

它適合你嗎? – rb512

+0

工作正常。抱歉耽擱了。 – jhamm

+0

沒問題,很高興它的工作! – rb512

回答

1

創建這就要求這種形式的操作的對象@crew_member,我想這將是一個新的動作:
@member = @project.build_crew_member(如果使用的是軌道3)或
@member = @project.crew_member.build(如果<軌道3)

然後在你的形式,使用fields_for表單標籤捕捉crew_member領域:
<%= f.fields_for :crew_member do |m|%>
<%= m.text_field :name%>