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

  • Home
  • SEARCH
  • 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 6697905
In Process

The Archive Base Latest Questions

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

I am using Formtastic and also using the accepts_nested_attributes_for ..here are models class Tournament

  • 0

I am using Formtastic and also using the accepts_nested_attributes_for ..here are models

class Tournament < ActiveRecord::Base
  has_many  :courts, :dependent => :destroy
  accepts_nested_attributes_for :courts, :allow_destroy => true

class Court < ActiveRecord::Base
  belongs_to :tournament

I need to have a form that one of the questions in the tournament form will determine how many courts i need to build. Here is my approach now

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

and in the view

<%= 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 |ct| %>
          <%= ct.input :name %>
        <% end %>

the problem with this approach is that I will always prompt the user with 25 fields when really i need only the amount they enter from the question “How many courts are available?”

Is there a way to do this or just add them with a link …any ideas

  • 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:30:55+00:00Added an answer on May 26, 2026 at 6:30 am

    There are certainly more than one ways to do this. Here is one.

    Have the 25 courts prebuilt in the new action and include them in the new page but do not display them (display:none). On the client side when the user enters/selects the number of courts then you display those number of court input fields. You will need very simple javascript for this. On the rais controller side you can ignore all the court fields that are beyond the number of courts selected by the user

    In your params the court fields would come in an as array. Use slice! to get rid of the extra courts.

    params[:tournament][:courts].slice!(0..params[:tournament][:number_courts].to_i) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using formtastic like this class Court < ActiveRecord::Base belongs_to :tournament end class
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'm trying to set up a many to many relationship using the has_many :through
I've been using formtastic in order to generate HTML forms on rails applications. My
f.input :some_model_values, :as => :select Using Formtastic, I need to set a text for
I am trying to setup a simple form using formtastic but running into a
Have a boolean field (using formtastic) and the value won't persist through an error.
I'm using formtastic to collect information from a form and post dirctly to an
I'm using formtastic & formtastic_cocoon to created a nested form. All seems to be

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.