Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6698585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:35:43+00:00 2026-05-26T06:35:43+00:00

I am using formtastic like this class Court < ActiveRecord::Base belongs_to :tournament end class

  • 0

I am using formtastic like this

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

and all works fine but on a failed validation of the tournament form the courts that were created are not preserving on a failed form.

here is my controller

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

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

i assumed that i could do something like this in the create but no go

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

partial

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

here is my create action

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

also here there the params just in case that will help

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

any ideas on how to preserve the data over a failed validation

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:35:43+00:00Added an answer on May 26, 2026 at 6:35 am

    I assume that when you try to create a Tournament, and the validation fails, its child Courts are not actually created. What you want is for the Court data that was entered on the Tournament form to be preserved across creation attempts. Does that sound correct?

    I haven’t used formatastic. However, if its API is really just comprised of wrappers around Rails form helpers, then I would give this a shot:

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

    Notice the change to the rendering of the court fields partial.

    When you attempt to create a tournament, this should render the court you built in TournamentsController.new. When the attempt fails, it should render the courts that were input during the first attempt.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Formtastic and also using the accepts_nested_attributes_for ..here are models class Tournament
(I am new to paperclip) I have this model hierarchy: base model: class QuestDescription
Using C#, I need a class called User that has a username, password, active
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
I've been using formtastic in order to generate HTML forms on rails applications. My
I'm trying to set up a many to many relationship using the has_many :through
I would like to use formtastic instead of standard Rails helpers for my forms,
I am trying to set up a form using Formtastic which has a drop
f.input :some_model_values, :as => :select Using Formtastic, I need to set a text for
I am using bootstrap-sass, formtastic-bootstrap, rails 3.2, and ruby 1.9.3p0 on a new project.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.