Rails + Materialize Select field

Asked

Viewed 335 times

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 to Rails, but it’s not working. It shows nothing, although it has data.

[BUT I DON’T SEE IT AS THE RIGHT SOLUTION]

  <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' applies an effect that would not be the same as the standard form, but the combo appears with the data.

  • Can you show the result of this? and the generated HTML?

  • Change your answer to the answer field, not the question field.

2 answers

1

This is the method with the parameters to 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 on to options, but must be passed on to html_options. So you have to pass one hash empty for options to pass the parameters of html_options.

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

1

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

You must initialize the select element as shown below. In addition, you will need a separate call for 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 should initialize it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.