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 8741183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:15:58+00:00 2026-06-13T11:15:58+00:00

I am creating a system that stores cards using Ruby/Rails/HAML – In this case

  • 0

I am creating a system that stores cards using Ruby/Rails/HAML – In this case there is a Card class that has many Colours (this is also a class). When creating and editing a card I am using the Cocoon gem to allow me to dynamically add colour associations.

The problem I am having is that in the Card model, a card is limited to having a maximum of 5 colours. Yet the interface allows adding of unlimited colours resulting in an error.

Is there a way in Cocoon to limit the number of associations that can be added to a form, so that this limit is not exceeded?

This is the code for a form to add/edit a Card

 = simple_form_for @card, multipart: true do |c|
  = c.input :name, label: "Name of the card"
  = c.input :cost, label: "Cost of the card"
  #colours
   = c.simple_fields_for :colours do |colour|
    = render "colour_fields", f: colour
   .links
    = link_to_add_association 'add colour', c, :colours

And this is the colour_fields form

.nested-fields
 = f.input :value, as: :select, collection: Colour::VALUES, selected: f.object.value, include_blank: false
 = link_to_remove_association "remove colour", f

Thanks in advance.

  • 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-06-13T11:15:59+00:00Added an answer on June 13, 2026 at 11:15 am

    I would use javascript for this. You can bind to an event that is triggered upon insert of a new item: on this event count how many items there are, and hide the link if needed.

    Likewise, when loading the page do the same.

    So in code that would look like:

     $(function() {
       function check_to_hide_or_show_add_link() {
         if ($('#colours .nested-fields:visible').length == 5) {
           $('#colours .links a').hide();
         } else {
           $('#colours .links a').show();
         }
       }
    
       $('#colours').on('cocoon:after-insert', function() {
         check_to_hide_or_show_add_link();
       });
    
       $('#colours').on('cocoon:after-remove', function() {
         check_to_hide_or_show_add_link();
       });
    
       check_to_hide_or_show_add_link();     
     });
    

    Something like this should work. Note this code is not tested 🙂

    Hope this helps.

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

Sidebar

Related Questions

I am creating an input system (using the windows messaging system) that I would
I'm creating xml-like mark-up language using System.Xml.XmTextWriter that will be read by a third
I'm creating an air traffic control system. I have a class called plane that
We are creating a system in Ruby on Rails and we want to be
I am creating a system that will store objects in an SQLite Database. I
I'm creating a system with a JavaScript client that will communicate with the server
So, I'm creating a system which manages all the drupal websites that sit within
I'm creating a log system ('outside' the application) that logs all the click on
I am creating an app that is a ticket sales system. On the checkout
I'm creating a directx application that relies on the system time (because it must

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.