select2 css takes too long to load - css

I'm using the nice Select2 for multiple select fields on my web applications search page. One can search for people by company/industry/school. Because there are hundreds of searchable values for each, it is taking time for the correct select2 css to load (about 1 second). The old ugly select fields can be seen in a flicker before the pretty select2 fields display. Attached are two screenshots that show the transition.
Here is my view code:
<%= form_tag('', :method => :get) do %>
<div class="row-fluid">
<div class="span4">
<label>What industries have they worked in?</label>
<%= select_tag "industry_ids", options_for_select((#visible_industries), params[:industry_ids]), { :placeholder => "Type to search...", :multiple => true, :id => "e3", :onchange => 'this.form.submit();' } %>
<%= hidden_field_tag :&, params[:industry_ids] %>
</div>
<div class="span4">
<label>What companies have they worked at?</label>
<%= select_tag "company_ids", options_for_select((#visible_companies), params[:company_ids]), { :placeholder => "Type to search...", :multiple => true, :onchange => 'this.form.submit();' } %>
<%= hidden_field_tag :&, params[:company_ids] %>
</div>
<div class="span4">
<label>Where did they go to school?</label>
<%= select_tag "school_ids", options_for_select((#visible_schools), params[:school_ids]), { :placeholder => "Type to search...", :multiple => true, :onchange => 'this.form.submit();' } %>
<%= hidden_field_tag :&, params[:school_ids] %>
</div>
<div class="row-fluid">
<% end %>
</div>
</div>
And controller code:
def people
#current_user = current_user
#visible_positions = Position.where{ is_visible.eq('true') }
#visible_educations = Education.where{ is_visible.eq('true') }
#visible_companies = #visible_positions.order("LOWER(company)").all.map { |p| [ p.company, p.company ] }.uniq
#visible_industries = #visible_positions.order("LOWER(industry)").all.map { |p| [ p.industry, p.industry ] }.uniq
#visible_schools = #visible_educations.order("LOWER(school)").all.map { |e| [ e.school, e.school ] }.uniq
#c = #visible_positions.where{company.in(my{params[:company_ids]})}.map(&:user_id)
#i = #visible_positions.where{industry.in(my{params[:industry_ids]})}.map(&:user_id)
#s = #visible_educations.where{school.in(my{params[:school_ids]})}.map(&:user_id)
unless #c.empty? && #i.empty? && #s.empty?
#users = User.find([#c,#i,#s].reject(&:empty?).reduce(:&))
end
end
Finally, I have this javascript in my assets directory (in addition to the select2 css):
$(document).ready(function(){
$('select').select2({
minimumInputLength: 1
});
});
What can I do to prevent this flicker from happening? Thank you.

I'll post my comment as an answer since it solved your issue.
My suggestion would be to hide the selects till the page is fully loaded (and select2 applied).
In the stylesheet, add a input {display:none;} rule to hide them (although visibility:hidden might be better). Then with jQuery override that rule $('input').css("display","inline");.

Related

event.stop(e) and returnfalse statemens are not working

<div class = "heading">
Add Service Order
</div>
<%= link_to "Back" ,:action => "list"%>
<div style="float: left;">
<%#= form_tag ({:action => 'create' do %>
<%= form_tag({:action => 'create'}, :multipart=>true, :id=> #add_service_order, :method => :post) do %>
<%= render :partial => 'form' %>
<br>
<center><%= submit_tag 'Save',:class=>"buttons",:id => "save_button", :onClick => "po_num();"%></center>
</br>
<% end %>
</div>
<script>
function po_num(){
//alert ("11")
var po = document.getElementById('add_service_order_service_order_no').value
//alert(po);
//return false;
if (po == "")
{
alert("Please fill PO Number");
Event.stop(e);
}
}
</script>
event.stop(e) and Returnfalse are not working.Whenever i am going to save without filling mandatory fields it's giving alert but it is not stoping there and it is going to next page and saving..How can i resolve this ?

How can I style simple form f.association check_boxes elements?

I'm trying to style a simple quiz. The image that I use is supposed be the background of the checkbox and the value.name should be on top (as a header) with the value.description as text below. Right now everything obviously is displayed next to each other. And I can't figure out a way to change it.
<%= simple_form_for #user_value, :method => 'post' do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<div class="form-inputs">
<%= f.association :value, label_method: lambda { |value| image_tag(cl_image_path value.photo.key) + "#{value.name} - #{value.description}"} , :label => "Select at least 10 values", as: :check_boxes, input_html: { class: "value-selector" }, item_wrapper_class: 'value-item'%>
</div>
<div class="form-actions">
<%= f.button :submit, "Continue" %>
</div>
<% end %>

Rails collection_select + bootstrap dropdown

<div class="dropdown">
...
<%= ff.collection_select :vendor_id, #vendors, :id, :vendor, :include_blank => true, class: "dropdown-menu" %>
...
</div>
I have a form and then I'm using fields_for as shown above to collect input. But I'm unable to see the stylized bootstrap dropdown as illustrated here. What changes should I be making to see the bootstrap stylized dropdown?
from
<%= ff.collection_select :vendor_id, #vendors, :id, :vendor, :include_blank => true, class: "dropdown-menu" %>
to
<%= ff.collection_select :vendor_id, #vendors, :id, :vendor, {:include_blank => true}, {class: "dropdown-menu"} %>
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

Ruby on Rails: Required input field CSS not showing

I have a form and I'm setting a field to be required before submitting, however nothing is showing up when I hit the Search button. What do I need to do to style the form?
<%= form_tag search_path, :method => :get, class: "form-search-home" do %>
<%= text_field_tag :q, :class => "term form-control" %>
<%= text_field_tag :loc, :class => "loc form-control", :required => true %>
<%= button_tag :type => :submit, :class => "btn" do %>Search<% end %>
<% end %>
Thanks!
nothing is showing up when I hit the Search button
The problem here is likely a Rails / HTML issue than CSS (as mentioned in your question)
Syntax
As pointed out in the comments, you have a series of problems with your code syntax, specifically with submit_tag & text_field_tag:
<%= form_tag search_path, method: :get, class: "form-search-home" do %>
<%= text_field_tag :q, nil, class: "term form-control" %>
<%= text_field_tag :loc, nil, class: "loc form-control", required: true %>
<%= submit_tag "Search", class: "btn" %>
<% end %>
This should fix any of the syntax issues you have on your form, allowing it to submit. The reason why it doesn't at the moment is likely down to the syntax being incorrect. If you use the above code, it should render the form correctly, allowing you to submit it as required!
--
CSS
CSS is cascading style sheets - meaning they're only meant to style your page. They can't fix any syntax, backend or HTML rendering issues - only how the HTML appears in the browser
If you've still got trouble with your CSS, you'll be best styling the form with the inputs inheriting from the main class styling:
#app/assets/stylesheets/application.css
form {
/* form code */
}
form input.required {
/* required form element styling */
}
Does your form code generate a valid HTML?
As far as I see from documentation, text_field_tag method has three arguments:
text_field_tag(name, value = nil, options = {})
Your example ommits the second argument (value), so may be that is the case. Wonder if this could help:
<%= text_field_tag :loc, nil, :class => "loc form-control", :required => true %>

How to add a class to select_tag in Ruby on Rails

Having difficulty adding a class tag to this:
<div class="field">
<%= label_tag(:school_or_org, "Are you a school or a non-profit organization?") %>
<%= select_tag(:school_or_org, options_for_select([ ['school', "school"], ['non-profit', "non-profit"] ])) %>
</div>
This is what I've tried, among others:
<div class="field">
<%= label_tag(:school_or_org, "Are you a school or a non-profit organization?") %>
<%= select_tag(:school_or_org, options_for_select([ ['school', "school"], ['non-profit', "non-profit"], {:class => 'form-control'} ])) %>
</div>
When I check it via firefbug, I don' see the class information, so clearly it's not being added.
How do I get it added so it shows up in the form?
Merci
Edit One
Do you mean like this:
<%= select_tag(:school_or_org, options_for_select([ ['school', "school"], ['non-profit', "non-profit"]] ), {:class => 'form-control'}) %>
It didn't work.
Just for reference, this is what I'm trying to have the select box look like: http://getbootstrap.com/css/#forms (look at Selects section). That's where I got 'form-control' from.
If you are using the select helper, you need an empty options hash followed by your html options like so:
select(object, method, choices, options = {}, html_options = {})
With your example, that would look like this:
<%= select(:school_or_org, options_for_select([ ['school', "school"], ['non-profit', "non-profit"]] ), {}, {:class => 'form-control'}) %>
If you are using the select_tag helper, you can add in the hash at the end as part of the options hash:
select_tag(name, option_tags = nil, options = {})
This works because any options that are not recognised are passed in as standard HTML attributes.
With your example , that would look like this:
<%= select_tag(:school_or_org, options_for_select([ ['school', "school"], ['non-profit', "non-profit"]] ), {:class => 'form-control'}) %>
If you want the class on the select box itself, then you have a parenthesis nesting error happening here. The {:class => 'form-control'} snippet needs to be outside of the parens for the options_for_select call.
If you want the class to be applied to the options then each array element needs the class declaration like so: ['non-profit', "non-profit", {:class => 'form-control'}]

Resources