Select option does not work in Rails using MaterializeCSS

此生再无相见时 提交于 2019-12-08 03:26:27

问题


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%>

回答1:


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.




回答2:


Just my two cents:

MaterializeCSS is in version 1.0.0 now, so instead of

$('select').material_select();

you will use

$('select').formSelect();


来源:https://stackoverflow.com/questions/38170589/select-option-does-not-work-in-rails-using-materializecss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!