0

나는이유효성 검사 실패시 하위 필드를 저장 하시겠습니까?

class Court < ActiveRecord::Base 
    belongs_to :tournament  
end 

class Tournament < ActiveRecord::Base 
    has_many :courts, :dependent => :destroy 
    accepts_nested_attributes_for :courts, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true 
    validates :name, :presence => true, :length => { :maximum => 100 } 
end 

처럼 formtastic 사용하고 모든 잘 작동하지만 대회의 실패 검증에 실패한 양식에 보존되지 않습니다 생성 된 코트를 형성한다. 여기

내 컨트롤러

def new 
    @tournament = Tournament.new 
    1.times do 
    @tournament.courts.build 
    end 

def create 
    @tournament = Tournament.new(params[:tournament]) 
나는이 같은 일을 할 수 있다고 가정

입니다 만들 수 없지만

<div class="nested_fields"> 
<%= f.input :name %> 
<%= f.input :address %> 
<%= f.input :city %> 
<%= f.input :state %> 
<%= f.input :zip %> 
<%= f.input :phone %> 
<%= f.input :contact_name %> 
<%= link_to_remove_fields "Remove Court", f %> 
</div> 

UPDATE ---

if params[:tournament][:courts_attributes] 
     params[:tournament][:courts_attributes].each { |attribute| 
      @tournament.courts.build(attribute) 
     } 
     end 


<%= semantic_form_for @tournament do |f| %> 
    <%= f.inputs do %> 
    <%= f.input :number_courts, :hint => "How many courts are available?" %> 

     <%= f.semantic_fields_for :courts do |builder| %> 
      <%= render :partial => "court_fields", :locals => { :f => builder } %> 
      <span class="links"> 
      <%= link_to_add_fields "Add More Court", f, :courts %> 
      </span> 
     <% end %> 

부분에는 이동

he 내 여기가 액션을 실패한 검증을 통해 데이터를 보존하는 방법에

Processing by TournamentsController#create as HTML 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xv+p7QdpkJdEUaTGqrKue63869hlwh3Zv1xvkO5qx6A=", "tournament"=>{"name"=>"", "sport_id"=>"1", "entry_fee"=>"", "start_date"=>"", "end_date"=>"", "number_courts"=>"", "courts_attributes"=>{"0"=>{"name"=>"hello", "_destroy"=>"false"}, "1318725283928"=>{"name"=>"asdfsadfas", "_destroy"=>"false"}, "1318725286739"=>{"name"=>"asdfasdfa", "_destroy"=>"false"}}, "available_times"=>"", "available_end_times"=>"", "min_games"=>"", "time_allowed"=>"1:15", "number_teams_per_bracket"=>"1", "gender_id"=>"1", "entry_deadline"=>"", "age_limit"=>"", "rules"=>"", "coach_meeting"=>"0", "meeting_location"=>"", "meeting_date"=>"", "future_tournament"=>"0", "private_service"=>"0", "add_blog"=>"0"}, "commit"=>"Create Tournament"} 

어떤 아이디어가 도움이 될 것입니다 단지 경우에 PARAMS을

def create 
    @tournament = Tournament.new(params[:tournament]) 
    respond_to do |format| 
    if @tournament.save 
     format.html { redirect_to @tournament, notice: 'Tournament was successfully created.' } 
     format.json { render json: @tournament, status: :created, location: @tournament } 
    else 
     format.html { render action: "new" } 
     end 

을 만드는 것입니다 재 난 때 가정

+0

양식을 게시 할 수 있습니까? 또한 '1x' ?? – bricker

+0

@bricker - 1 시간 동안 죄송합니다 ... 나는 25 번에서 그것을 바꿔서 루프를 제거하는 것을 잊었습니다. 지금 양식을 붙이십시오. – Trace

+1

그래, 내가 처음으로 보았을 때 그게 어리석은 것처럼 보였습니다. 그것. – bricker

답변

2

토너먼트를 만들려고 시도했지만 검증이 실패하면 자식 코트가 실제로 생성되지 않습니다. 원하는 것은 토너먼트 양식에 입력 된 법원 데이터가 생성 시도를 통해 보존되도록하는 것입니다. 소리가 맞습니까?

나는 formatastic을 사용하지 않았습니다. 법원의 렌더링에 변화가 일부 필드

<%= f.semantic_fields_for :courts do |builder| %> 
     <%= render :partial => "court_fields", :collection => @tournament.courts, :locals => { :f => builder } %> 
     <span class="links"> 
     <%= link_to_add_fields "Add More Court", f, :courts %> 
     </span> 
    <% end %>  

주의 사항 : 자사의 API가 정말 래퍼로 구성되어 있습니다 그러나, 주위 폼 헬퍼 레일, 나는이 주사를한다.

토너먼트를 만들려고하면 법원을 TournamentsController.new으로 렌더링해야합니다. 시도가 실패하면 첫 번째 시도에서 입력 된 법원을 렌더링해야합니다.

+0

어떤 이유에서든 법원에서 전혀 절약하지 않을 것입니다. – Trace

+0

토너먼트는 – Trace

+0

입니다. 토너먼트가 저장하더라도 법원이 저축하지 않는다는 것을 의미합니까? 또는 그들의 토너먼트가 저장하지 않아도. 전자의 경우 문제입니다. 후자의 경우 예상되는 동작입니다. – maxenglander