2012-11-06 3 views
0

이 양식을 제출하면 Members 테이블에 두 개의 동일한 레코드 (양식의 fields_for 부분)가 작성됩니다. 그 일이 왜 일어나는지 이해하도록 도와주세요.양식 제출시 fields_for가 2가 아닌 동일한 레코드를 만드는 이유는 무엇입니까?

기본 설정은 다음과 같습니다. Comp에는 많은 팀이 있고 팀에는 많은 구성원이 있습니다. 새 팀을 만들 때 첫 번째 멤버는 팀의 총무가되어야합니다 (즉, Members 테이블의 secretary_flag 필드를 TRUE로 설정해야 함). 아래 양식은 새 팀을 만들고 첫 번째 팀원을 만들고 그 팀원을 비서로 표시하십시오.

컨트롤러 :

def new 
    @comp = Comp.find(params[:comp_id])  
    @team = @comp.teams.new 
    @team.members.build 
end 

def create 
    @comp = Comp.find(params[:comp_id]) 
    @team = @comp.teams.create(params[:team]) 
    if @team.update_attributes(params[:team]) 
     flash[:success] = "Team added successfully." 
     redirect_to new_comp_team_member_path(@comp,@team) 
    else 
     render 'new' 
    end 
end 

양식보기 :

resources :comps do 
    resources :teams do 
     resources :members 
    end 
end 

그리고 내 모델 :

comp.rb

<%= form_for [@comp,@team] do |builder| %> 
    <%= builder.label :team_name, "Team name" %> 
    <%= builder.text_field :team_name %>   
    <%= builder.fields_for :members do |f| %> 
     <%= f.label :member_email, "Email address of team secretary" %> 
     <%= f.text_field :member_email %> 
     <%= f.hidden_field :secretary_flag, :value => 1 %> 
    <% end %> 

<%= builder.submit "Create new team" %> 

<% end %> 

그리고 내 경로에

:

,
attr_accessible :teams_attributes 
has_many :teams, :dependent => :destroy 
accepts_nested_attributes_for :teams, :allow_destroy => :true 

team.rb :

attr_accessible :members_attributes 
belongs_to :comp 
has_many :members 
accepts_nested_attributes_for :members 

member.rb :

belongs_to :team 
+0

변경하십시오. <% = form_for [@ comp, @ team] do | builder | %>'~'<% = form_for @team do | 빌더 | %>' – Thanh

+0

벌써 시도했다. 이 오류가 발생합니다 : ID없이 Comp를 찾을 수 없습니다. 팀이 comps의 중첩 된 경로이기 때문에 그게겠습니까? – Josh

+0

새 팀 및 회원을 만드는 데 사용 된 경로는 무엇입니까? – Thanh

답변

0

내가 아주 기본적인이었다 알아 냈 솔루션. 내 생성 동작에서 실수로 넣어 :

if @team.update_attributes(params[:team]) 

가 있었어야 때

if @team.save(params[:team]) 

그 생각이 개 동일한 기록을 만든 이유를 난 아직도 이해하지 않는다, 그러나 그것은 작동합니다.

관련 문제