Rails + Materialize Select field

0

Diplomats, I have the following problem:

  <div class="input-field">
    <%= f.label :kind_id %>
    <%= collection_select(:contact, :kind_id, @kind_options_for_select, :id, :description, class: 'input-field') %>
  </div>

I'm trying to apply the materialize select class in rails, but it's not working. It shows nothing, although it has data.

[SOLUTION BUT I DO NOT SEE HOW TO CORRECT IT]

  <div class="input-field">
    <%= collection_select(:contact, :kind_id, @kind_options_for_select, :id, :description, { prompt: "Choose a Kind" }, { class: 'browser-default' }) %>
  </div>

This class: 'browser-default' it applies an effect that would not be the same as the default form, but the combo with the data appears.

    
asked by anonymous 09.03.2017 / 14:48

2 answers

1

This is the method with the parameters that must be passed:

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

You're using it this way:

collection_select(:contact, :kind_id, @kind_options_for_select, :id, :description, class: 'input-field')

The argument class: 'input-field' is being passed to options , but must be passed to html_options . So you have to pass% empty% to options in order to pass the parameters of hash .

collection_select(:contact, :kind_id, @kind_options_for_select, :id, :description, {}, { class: 'input-field' })
    
11.03.2017 / 12:23
1

Your problem is not related to Rails, but to materialize, according to the select documentation itself:

  

You must initialize the select element as shown below. In addition, you'll need a separate call to any select elements generated dynamically by your page.

  $(document).ready(function() {
    $('select').material_select();
  });

That is, every time you create or change your select dynamically you should initialize it.

    
21.05.2017 / 14:53