在验证失败时保存子字段?

时间:2011-10-16 00:22:12

标签: ruby-on-rails ruby-on-rails-3 formtastic nested-attributes

我正在使用像这样的formtastic

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

并且一切正常但是在对比赛形式的验证失败时,所创建的法院并未保留在失败的表格上。

这是我的控制器

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

def create
  @tournament = Tournament.new(params[:tournament])

我认为我可以在创作中做这样的事情,但不要去

    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 %>

部分

<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 ---

这是我的创建动作

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

此处还有params以防万一,这将有助于

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"}

有关如何通过失败的验证保留数据的任何想法

1 个答案:

答案 0 :(得分:2)

我假设当您尝试创建锦标赛并且验证失败时,实际上并未创建其子Courts。您想要的是在锦标赛表单上输入的Court数据在创建尝试中保留。这听起来不错吗?

我没有使用过formatastic。但是,如果它的API实际上只是由Rails表单助手组成的包装器,那么我会给它一个镜头:

    <%= 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 %>    

请注意法院字段呈现的部分更改。

当您尝试创建锦标赛时,这应该会渲染您在TournamentsController.new内构建的球场。当尝试失败时,它应该呈现在第一次尝试期间输入的法院。