質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

1回答

773閲覧

フォロー機能(いいね機能と一緒)のcreate.js.erb ,destroy.js.erb のコードをコメントアウトしてもなぜか動く。

YutoKubo

総合スコア13

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/12/13 01:58

編集2020/12/14 01:57

いいね機能を参考にし中身は一緒でフォロー機能を作成したのですが、create.js.erb ,destroy.js.erb のコードに関わらず動作することに気づきコメントアウトしてみたところ、やはり一見フォロー機能は動作しているように見えます。
要するに非同期処理があたかも remote true だけで動いているということです。

(※立て続けの質問になるのですが、フォローボタンをおしたとき出現するフォロー解除ボタンが画像のように下に一段ずれてしまっています。)

ご指摘お願いします。

![![イメージ]

<フォローした時(create)のログ>

Started POST "/communities/1/follows" for ::1 at 2020-12-14 10:51:45 +0900 Processing by FollowsController#create as JS Parameters: {"community_id"=>"1"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/controllers/follows_controller.rb:5:in `create' (0.1ms) begin transaction ↳ app/controllers/follows_controller.rb:5:in `create' Community Load (0.1ms) SELECT "communities".* FROM "communities" WHERE "communities"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/controllers/follows_controller.rb:5:in `create' Follow Exists? (0.1ms) SELECT 1 AS one FROM "follows" WHERE "follows"."community_id" = ? AND "follows"."user_id" = ? LIMIT ? [["community_id", 1], ["user_id", 1], ["LIMIT", 1]] ↳ app/controllers/follows_controller.rb:5:in `create' Follow Create (0.5ms) INSERT INTO "follows" ("created_at", "updated_at", "user_id", "community_id") VALUES (?, ?, ?, ?) [["created_at", "2020-12-14 01:51:46.010978"], ["updated_at", "2020-12-14 01:51:46.010978"], ["user_id", 1], ["community_id", 1]] ↳ app/controllers/follows_controller.rb:5:in `create' (1.5ms) commit transaction ↳ app/controllers/follows_controller.rb:5:in `create' Redirected to http://localhost:3000/ Completed 200 OK in 14ms (ActiveRecord: 2.5ms | Allocations: 6418) Started GET "/" for ::1 at 2020-12-14 10:51:46 +0900 Processing by CommunitiesController#index as HTML Rendering communities/index.html.erb within layouts/application Community Load (0.2ms) SELECT "communities".* FROM "communities" ↳ app/views/communities/index.html.erb:3 User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/views/communities/index.html.erb:16 Follow Exists? (0.3ms) SELECT 1 AS one FROM "follows" WHERE "follows"."user_id" = ? AND "follows"."community_id" = ? LIMIT ? [["user_id", 1], ["community_id", 1], ["LIMIT", 1]] ↳ app/models/user.rb:11:in `following?' (0.2ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."community_id" = ? [["community_id", 1]] ↳ app/views/communities/index.html.erb:25 Follow Exists? (0.1ms) SELECT 1 AS one FROM "follows" WHERE "follows"."user_id" = ? AND "follows"."community_id" = ? LIMIT ? [["user_id", 1], ["community_id", 2], ["LIMIT", 1]] ↳ app/models/user.rb:11:in `following?' (0.1ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."community_id" = ? [["community_id", 2]] ↳ app/views/communities/index.html.erb:25 Follow Exists? (0.1ms) SELECT 1 AS one FROM "follows" WHERE "follows"."user_id" = ? AND "follows"."community_id" = ? LIMIT ? [["user_id", 1], ["community_id", 3], ["LIMIT", 1]] ↳ app/models/user.rb:11:in `following?' (0.1ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."community_id" = ? [["community_id", 3]] ↳ app/views/communities/index.html.erb:25 Follow Exists? (0.2ms) SELECT 1 AS one FROM "follows" WHERE "follows"."user_id" = ? AND "follows"."community_id" = ? LIMIT ? [["user_id", 1], ["community_id", 4], ["LIMIT", 1]] ↳ app/models/user.rb:11:in `following?' (0.3ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."community_id" = ? [["community_id", 4]] ↳ app/views/communities/index.html.erb:25 Follow Exists? (0.3ms) SELECT 1 AS one FROM "follows" WHERE "follows"."user_id" = ? AND "follows"."community_id" = ? LIMIT ? [["user_id", 1], ["community_id", 5], ["LIMIT", 1]] ↳ app/models/user.rb:11:in `following?' (0.1ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."community_id" = ? [["community_id", 5]] ↳ app/views/communities/index.html.erb:25 Rendered communities/index.html.erb within layouts/application (Duration: 21.8ms | Allocations: 11134) [Webpacker] Everything's up-to-date. Nothing to do Completed 200 OK in 32ms (Views: 28.6ms | ActiveRecord: 2.5ms | Allocations: 19319)

<index.htnl.erb>

<div class="container"> <div class="row"> <% @communities.each do |community| %> <div class="col"> <%= link_to community_path(community) do %> <div class="card" style="width: 14rem;"> <div class="card-image"> <%= attachment_image_tag community, :intro_image, fallback:"no-image.jpg", class:"rounded-top" %> </div> <div class="card-body"> <%= community.title %> <%= community.introduction%> </div> <% if current_user.present? && current_user.following?(community) %> <%= link_to community_follows_path(community.id), method: :delete, remote: true do %>              <button type="button" class="btn btn-primary btn-sm">フォロー解除</button> <%end%> <% else %> <%= link_to community_follows_path(community.id), method: :post, remote: true do %> <button type="button" class="btn btn-primary btn-sm">フォロー</button> <%end%> <%end%> <%= community.follows.count %> </div> <%end%> </div>   <%end%> </div> </div>

<follows.controller.rb>

class FollowsController < ApplicationController def create @follow = current_user.follows.create(community_id:params[:community_id]) redirect_back(fallback_location: root_path) end def destroy @community = Community.find(params[:community_id]) @follow = current_user.follows.find_by(community_id: @community.id) @follow.destroy redirect_back(fallback_location: root_path) end end

<create.js.erb>

// $("<%= @id_community %>").html('<%= escape_javascript(render("follows/follow", community: @community))%>');

<destroy.js.erb

// $("<%= @id_community %>").html('<%= escape_javascript(render("follows/follow", community: @community))%>');

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

redirect_back(fallback_location: root_path)
しているので view までいってないのではないでしょうか。

create/destroyのログに Redirected to ... とありませんか?

投稿2020/12/13 19:11

neko_daisuki

総合スコア2090

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問