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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:17:44+00:00 2026-06-15T04:17:44+00:00

The answer on this question has provided me with a nice roadmap for how

  • 0

The answer on this question has provided me with a nice roadmap for how to generate select tags with data from a collection on an association.

This works nicely and everything is going great.

The issue I have now is, how do I handle an empty collection?

With the regular :type => :input, I can just specify :nil => "Some nil message here".

But that doesn’t seem to work for the collection, and to make matters worse, when there is nothing in the collection it seems to be displaying some integers (i.e. 1 and 2). I am assuming those are the IDs from the previously displayed objects in the collection, but for obvious reasons that doesn’t make much sense.

Any ideas on how I can handle an empty collection with this gem?

Thanks.

Edit 1:

One alternative is to just put my original best_in_place helper tag inside an if statement for when a collection is not nil. But then how does the user edit it when it is blank? Perhaps there may be no way to handle this, because it would involve creating a new record in the collection.

  • 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-15T04:17:45+00:00Added an answer on June 15, 2026 at 4:17 am

    I use a “workaround” for the empty options in a select tag, it could help you:

    :type => :select, :collection => @my_colletion || [[I18n.t('common.none'), -1]]
    

    When @my_colletion is nil, it shows a choice named ‘None’ with id = -1 (wich is not that bad to handle in the backend).

    This part of code assumes the @my_collection is an array of arrays like [ [key, value], [key, value], ... ] OR nil.

    Although, if you want your MyModel.all collection to fit the conditions for best_in_place, you can use the following:

    @my_collection = MyModel.all.map{ |object| [object.name, object.value] } 
    # => this returns an array like [ [key, value], [key, value], ... ]
    # => returns an empty array (NOT nil) if there is no entry in the DB
    

    About the -1 id:
    Using the -1 id as ‘none’ is easy because you don’t need to explicitly handle the value nil (tests, etc). With the -1 id, you can use the following:

    MyModel.where(id: params[:id]).first # => Returns the first object that has the params[:id]
    # if params[:id] is -1, it will return nil and not raise an error.
    

    I hope it helped 🙂

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

Sidebar

Related Questions

An answer to this question has led me to look into using Event Tracing
This question has developed off an answer here . My question therefore is what
I know this question has been posted before... but I haven't found any answer
I'm sure this question has a simple enough answer, but I can't seem to
I'm hoping this question has a very simple answer. I can think of ways
Ok, so this question has been asked before here . In the response/answer to
This question already has an answer here: view sql that linq-to-sql produces 3 Answers
This question already has an answer here: What are Extension Methods? 5 answers Usage
I know this question has been asked many times here, but no answer has
This question has been asked here before . The accepted answer was probably obvious

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.