0
0

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 3 years have passed since last update.

非同期通信時 POSTではなくGET呼び出しになる

Last updated at Posted at 2021-09-18

#はじめに
非同期通信を使用して、○○.js.erbを呼び出して、javascriptの処理を実行させるため、以下のように処理を書いた。
postメソッドを指定したが、なぜかgetメソッドで呼び出されてしまい、○○.js.erbではなく○○.html.erbが呼び出された。

test.html.erb
<%= link_to "ボタン", hoge_path(1), id: "btn", method: :post, remote: true %>

#解決方法
以下をapplication.htmlのヘッダーへ追加

<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
application.html.erb
<!DOCTYPE html>
<html>
  <head>
    <title>hoge</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= stylesheet_link_tag 'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> <!-- 追記 -->
  </head>
・
・
・
</html>

#まとめ
非同期通信を実現するために必要なライブラリを読み込む必要があるが
それを読み込む記述がapplication.html.erbから漏れていた。

#参考
https://qiita.com/yuukinakamura0925/items/d4c7ee7b705c934f0e4f

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?