1
3

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.

*.js.erbファイルは読み込めているのに表示されないときの対処方法

Posted at
  • 「Completed 200 OK」となっているのに*.js.erbに書いたjavascriptのコードが動かないとき。
  • javascriptのエラーも発生していないとき。

原因

https://github.com/rails/rails/issues/33115
どうやらrails-ujsが求めているContent-Typeが一致しないことが原因っぽいです。

対策

【対策1】レイアウトファイル名を変更する

views/layouts/*.erb

views/layouts/*.html.erb

###【対策2】render時にlayout: falseを指定する

*_controller.rb
  def create
    @sending_user = User.find(current_user.id)
    @receiving_user = User.find(params[:user_id])
    new_like_balance = @sending_user.like_balance - 1
    @sending_user.update(like_balance: new_like_balance)
    @like = Like.create(sender_id: @sending_user.id, receiver_id: @receiving_user.id)
    # ここ
    render formats: :js, layout: false
  end

どちらの方法でも解決できました。

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?