28
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

erbからjavascriptに@userのidを渡す

Posted at

data属性を使う

erbと別ファイルのjavascriptにuserのidなどを渡したいときの書き方です。

erbファイル

<button id="hoge" 
        class="hoge" 
        type="button" 
        data-user-id="<%= @user.id %>" 
        data-post-id="<%= @post.id %>">
  削除
</button>

javascriptファイル

  const button = document.getElementById('hoge');

  const userId = button.dataset.userId;
  const postId = button.dataset.postId;

  console.log('User ID:', userId);
  console.log('Post ID:', postId);

ありがとうございました。

参考記事

データ属性の使用

28
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?