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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby on Rails

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

ログイン

ログインは、ユーザーがコンピューターシステムにアクセスするプロセスの事を呼びます。

Q&A

0回答

1227閲覧

railsのbefore_action :authenticate_user!が作動しない。

asato2000

総合スコア22

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby on Rails

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

ログイン

ログインは、ユーザーがコンピューターシステムにアクセスするプロセスの事を呼びます。

0グッド

0クリップ

投稿2021/01/11 08:33

前提・実現したいこと

現在webアプリを作っているのですが、deviseを使っているのですがdeviseのbefore_action :authenticate_user!がいいね機能で作用しないです。いいねはログインをしている人しかできないようにしたいです。posts/indexはログインしてなくての見れるようにしたいです。理想はいいねボタンを押すとログインフォームに飛ぶようにしたいです。
⬇️ ログインしていない状態です。いいね!ボタンを押すと何もならないです。

ログインしていない状態です。

該当のソースコード

posts/controller class PostsController < ApplicationController before_action :authenticate_user!, only: [:new,:create,:edit,:update,:destroy] before_action :find_post, only: [:show, :edit, :update, :destroy] before_action :correct_user, only: [:destroy,:edit] def index @posts = Post.order(id: :desc).page(params[:page]).per(8) end def show end def new @post = Post.new end def create @post = current_user.posts.build(post_params) if @post.save flash[:success] = "正常に投稿されました!" redirect_to posts_path else flash.now[:danger] = "正常に投稿できませんでした。" render :new end end def edit end def update if @post.update(post_params) flash[:success] = 'ノートは正常に更新されました' redirect_to root_path else flash.now[:danger] = 'ノートは更新されませんでした' render :edit end end def destroy @post.destroy flash[:success] = 'メッセージを削除しました。' redirect_back(fallback_location: root_path) end private def post_params params.require(:post).permit(:content,:title,:teacher_name,:subject,:post_image) end def find_post @post = Post.find(params[:id]) end def correct_user @post = current_user.posts.find_by(id: params[:id]) unless @post redirect_to new_user_session_path end end end
favorites/controller class FavoritesController < ApplicationController before_action :authenticate_user! def create @post = Post.find(params[:post_id]) @favorite = current_user.favorites.create(post_id: params[:post_id]) end def destroy @post = Post.find(params[:post_id]) @favorite = current_user.favorites.find_by(post_id: @post.id) @favorite.destroy end end
_favorite_button <%if user_signed_in? %> <% if current_user.already_favorited?(post) %> <%= link_to post_favorites_path(post.id), method: :delete, remote: true do %> <i class="fas fa-heart"></i> <%end%> <%else%> <%= link_to post_favorites_path(post.id),method: :post, remote: true do %> <i class ="fas fa-heart"></i> <%end%> <%end%> <% else %> <%= link_to post_favorites_path(post.id),method: :post, remote: true do %> <i class="fas fa-heart"></i> <% end%> <% end %> <%=post.favorites.count%>
post/index <div class="row row-cols-md-3"> <% @posts.each do |post| %> <div class="col-md-3"> <div class="card"> <%= attachment_image_tag post, :post_image, class: "card-img-top"%> <div class="card-body"> <h4 class="card-title"><%=post.title%></h4> <h6 class="card-subtitle text-primary"><%=post.subject%></h6> <h6 class="card-subtitle text-muted"><%=post.teacher_name%></h6> <p class="card-text"><%=post.content%></p> <%= link_to "詳細", post_path(post), class: "btn btn-info"%> <div class="text-center-1"> <% if current_user == post.user %> <%= link_to "削除", post, method: :delete, data: { confirm: "You sure?" }, class: 'btw' %> <%= link_to "編集", edit_post_path(post), class: "bta"%> <%end%> <div id="favorites_buttons_<%= post.id %>"> <%= render partial: 'favorites/favorite_button', locals: { post: post} %> </div> </div> </div> </div> </div> <% end %> </div> <%=paginate @posts %>

是非お願いします。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問