Search code examples
ruby-on-railsform-formaterialize

Select option does not work in Rails using MaterializeCSS


I have a select option in my form_for in rails. It was working before I installed materialize but after installing it, it doesnt work anymore. Please see my code below:

Game model

class Game < ActiveRecord::Base

    GENRES = ['Action', 'Adventure', 'RPG', 'Simulation', 'Strategy', 'Sports', 'Others']
    PLATFORMS = ['3DS', 'WII U', 'NX', 'PC', 'Playstation', 'XBOX', 'PS Vita', 'Mobile', 'Others']

end

new.html.erb

<h1> Add Game </h1>

<%= form_for :game, url: games_path do |f| %>
    Title: <%= f.text_field :title %> <br />
    Genre: <%= f.collection_select :genre, Game::GENRES, :to_s, :to_s, :include_blank => true %> <br />
    Platform: <%= f.collection_select :platform, Game::PLATFORMS, :to_s, :to_s, :include_blank => true %> <br />
    Release Date: <%= f.date_field :release_date %> <br />
    Progress: <%= f.number_field :progress %> <br />
    Rating: <%= f.number_field :rating %> <br />

    <%= f.submit 'Add Game', class: 'add_game_button' %>
<%end%>

Solution

  • MaterializeCSS uses a custom implementation of select element, that you have to initialize manually, using jQuery:

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

    For more details, check MaterializeCSS docs.