javascriptの''の中で変数を使う方法が知りたいです。javascriptでは式展開が使えないということですが、やはり最新のES6を使って式展開をする方法以外はないでしょうか?具体的には下記の場所で変数を使いたいです。よろしくお願いします。
$('ここで変数@indexを使いたい').html("<%= j(render partial: 'like', locals: { post: @post,i: @index,like:@likes }) %>");
@indexの定義部分
@index = params[:index]
viewファイル
<% if user_signed_in? %> <% if post.like_user(current_user.id) %> <%= button_to "/posts/#{post.id}/unlike",params:{index: i}, method: :delete, id: i, remote: true do %> <%= image_tag("icon_red_heart.svg") %> <span> <%= post.likes_count %> </span> <% end %>
viewファイルで出力されたHTML
each_with_indexでidをふっているので、下記と同じものでidだけが異なるものがひとつのページに多くあるというイメージです。
<form class="button_to" method="post" action="/posts/16/like" data-remote="true"><button id="0" type="submit"> <img src="/assets/icon_heart-ce229fb5b842bba5a3bf498493eecc5a.svg" alt="Icon heart" /> <span> 0 </span> </button><input type="hidden" name="authenticity_token" value="blU6kMq62zJ/HKQ5dmLC0ymeRROBt6cq67/rrcIV2J5QrasNijT6Hx9Zdx/eymsZXM7FrBOejLMkp3MzQZZ8yA==" /><input type="hidden" name="index" value="0" /></form>
私のやりたいことはviewファイルのid: iをjsファイルで認識してくれることです!
回答4件
あなたの回答
tips
プレビュー