我在一个rails应用程序中的视图中有一个button_to
函数,该应用程序当前在控制器中调用一个方法,但我也喜欢它执行一些javascript。我想提示用户输入一些我将发送回控制器并稍后使用的信息
这是我目前的代码。
<%= button_to 'Mark Learned' , song_rank_marklearned_path(song_rank_id: song_rank.id), remote: true , method: :post, onclick: "marklearned(#{song_rank.id})" %>
在html.erb文件中的脚本标记内,您可以编写一些自定义JS。
<script>
function marklearned() {
var songRankId = <%= song_rank.id %> // This can reference your helper_method or instance variable
// fetch call to post to your db
}
</script>