我正在使用来自控制器和turbolinks的js进行响应我没有加载mdl框架类。
控制器
def search
...
respond_to do |format|
format.html {redirect_to foods_path}
format.json {render json: @foods}
format.js
end
end
使用此按钮我在app.coffee中调用了一个函数 按钮
%button.mdl-button.mdl-js-button.mdl-js-ripple-effect#select-food{data:{id:"#{food.id}", title:"#{food.title}", status:"#{food.aasm_state}", carbohydrates:"#{food.carbohydrates}"}} Select
app.coffee
$("#select-food").on "click", (ev)->
...
# Geta data attr of button
id = $(this).data('id')
title = $(this).data('title')
carbohydrates = $(this).data('carbohydrates')
status = $(this).data('status')
# Append information in the table
$("#foods-table").append("<tr><input type='hidden' value='#{id}' name='foods[]'><td class='mdl-data-table__cell--non-numeric'>#{title}</td><td>#{parseInt(carbohydrates)}grs</td><td>#{status.toUpperCase();}</td><td><div class='mdl-textfield mdl-js-textfield mdl-textfield--floating-label' style='width: 5em;padding:0px;'><input type='number' name='quantity[]' class='mdl-textfield__input' placeholder='Quantity'></div><td>Total</td><td><button class='mdl-button mdl-js-button mdl-button--icon mdl-button--colored'><i class='material-icons'>delete</i></button></td></tr>")
...
application.cofee
$(document).on "page:load page:fetch ready turbolinks:load", ()->
...
componentHandler.upgradeDom()
我希望我之前解释得更好(我为此付出了很多努力),非常感谢你!