5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

data-params で POSTリクエストパラメタをいい感じに設定する

Last updated at Posted at 2016-02-23

Stack overflow で発見したので、備忘のためにメモ。

link_to "Add to cart", cart_path, 
    data: { method: :post, remote: true, params: { product_id: 123 }.to_param }

と書いておくと、下記HTMLが生成される。

<a data-params="product_id=123" data-remote="true" data-method="post" 
    href="/cart" rel="nofollow">Add to cart</a>

「Add to cart」をクリックすると、product_id=123 をパラメタとして /cart に xhr で POST リクエストされる。

芸が細かい。

5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?