前提・実現したいこと
ここに質問の内容を詳しく書いてください。
rails tutorialを参考にしながらインスタグラム風のアプリを作成しています。
フォロー機能の実装中ですが、フォローボタンをAjaxで非同期通信で実装したいのですが、リロードボタンを押さないと反映されません。
発生している問題・エラーメッセージ
該当のソースコード
gem file gem 'rails', '5.1.6' gem 'bcrypt', '3.1.12' gem 'faker', '1.7.3' gem 'carrierwave', '1.2.2' gem 'mini_magick', '4.7.0' gem 'will_paginate', '3.1.6' gem 'bootstrap-will_paginate', '1.0.0' gem 'bootstrap-sass', '3.3.7' gem 'puma', '3.9.1' gem 'sass-rails', '5.0.6' gem 'uglifier', '3.2.0' gem 'coffee-rails', '4.2.2' gem 'jquery-rails', '4.3.1' #gem 'turbolinks', '5.0.1' #gem 'jquery-turbolinks' gem 'jbuilder', '2.7.0' gem 'rails-i18n' gem 'ffi', '1.11.1'
application.html.erb <!DOCTYPE html> <html> <head> <title><%= full_title(yield(:title)) %></title> <%= csrf_meta_tags %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> <%= render 'layouts/shim'%> </head> <body> <%= render 'layouts/header' %> <% flash.each do |message_type,message| %> <div class = "alert alert-<%= message_type %>"><%= message %></div> <%end%> </header> <div class="container"> <%= yield %> <%= render 'layouts/footer'%> </div> </body> </html>
application.js // This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's // vendor/assets/javascripts directory can be referenced here using a relative path. // // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. JavaScript code in this file should be added after the last require_* statement. // // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // //= require jquery //= require jquery_ujs //= require_tree .
create.js.html $("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>"); $("#followers").html('<%= @user.followers.count %>'); destroy.js.html $("#follow_form").html("<%= escape_javascript(render('users/follow')) %>"); $("#followers").html('<%= @user.followers.count %>');
relationships.controller.rb class RelationshipsController < ApplicationController before_action :logged_in_user #フォローする def create @user = User.find(params[:followed_id]) current_user.follow(@user) respond_to do |format| format.html{redirect_to @user} format.js end end #フォロー解除 def destroy @user = Relationship.find(params[:id]).followed current_user.unfollow(@user) respond_to do |format| format.html{redirect_to @user} format.js end end end
試したこと
検索している中で、turbolinksを消してbundleをやれば解消されるという記事をいくつか見て試しましたが、うまくいきません。
補足情報(FW/ツールのバージョンなど)
Rails 5.1.6
ここにより詳細な情報を記載してください。
回答3件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2019/12/06 10:05
2019/12/08 12:39
2019/12/09 10:42
2019/12/10 13:30
2019/12/11 13:29
2019/12/13 01:12
2019/12/13 01:31
2019/12/13 09:45
2019/12/16 07:44
2019/12/17 11:09