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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:26:49+00:00 2026-06-16T14:26:49+00:00

I’m using select2 on a form. But want to use it throughout the application.

  • 0

I’m using select2 on a form. But want to use it throughout the application.

I have this part in my application.html.erb

<script type="text/javascript">
  $(document).ready(function() {
    <%= yield :document_ready %>
  });
</script>

And this in my view:

<% content_for :document_ready do %>
  $("#menu_category_id").select2({minimumResultsForSearch: 10});
<% end %>

and this is the actual input in the view:

<%= f.input :menu_category_id, :collection => @product.menu.menu_categories, 
  :label_method => :name, :value_method => :id, :label => "Category" %>

This works without any problems. However I don’t want to repeat this in all the views and would like to handle that with simple_view.

Best would be that if I use:

<%= f.input :menu_category_id, :collection => @product.menu.menu_categories, 
:label_method => :name, :value_method => :id, :label => "Category", 
:select2 => 'minimumResultsForSearch: 10' %>

it would automatically put it in the :document_ready part. So if there are 3 selects on a page it would do it for all of them with all the options that are specified in :select2

Is this possible with custom inputs? Or any other way?

  • 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-16T14:26:50+00:00Added an answer on June 16, 2026 at 2:26 pm

    I don’t think you need a custom input or the content_for helpers here. Assign a CSS class to the inputs you want select2 for, say with_select2. Next, assign them a data attribute specifying the minimumResultsForSearch value, say data-minimum-results. On document.ready then loop through all the inputs with class with_select2 and call select2 on them with the minimumResultsForSearch from the data attribute.

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

Sidebar

Related Questions

I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I

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.