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

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

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

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

リダイレクト

プログラムの入力元や出力先を通常とは別の場所に転送させることをリダイレクトと呼びます。

Q&A

解決済

1回答

2181閲覧

ユーザー編集機能でログインしていないユーザーをリダイレクトさせたい

denisov

総合スコア6

Devise

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

リダイレクト

プログラムの入力元や出力先を通常とは別の場所に転送させることをリダイレクトと呼びます。

0グッド

0クリップ

投稿2021/06/06 23:51

編集2021/06/08 22:13

□解決したいこと

deviceを用いてログインしていないユーザーもしくは権限のないユーザーがユーザー編集ページアクセスした際に、トップページにリダイレクトされる機能を実装しております。編集機能は実装済ですが、リダイレクト機能がうまく起動しません。

まずは、ログインしていないユーザーをログインページにリダイレクトする記述をしたいのですが、deviseの場合どのように記述すればいいかがわかりません。

環境
rails (6.0.3.7)
devise (4.8.0)

ご意見・アドバイスいただきたくお願い申し上げます。

ruby

1users/registrations_controller.rb 2 3class Users::RegistrationsController < Devise::RegistrationsController 4 before_action :authenticate_user! 5 before_action :configure_account_update_params, only: [:update] 6 7 protected 8 9 def update_resource(resource, params) 10 resource.update_without_password(params) 11 end 12 13 def after_update_path_for(_resource) 14 root_path 15 end 16 17 def configure_account_update_params 18 devise_parameter_sanitizer.permit(:account_update, keys: [:nickname, :last_name, :first_name, :introduction]) 19 end 20end

ruby

1application_controller.rb 2 3class ApplicationController < ActionController::Base 4 before_action :authenticate_user!, only: [:new,:edit] 5 before_action :configure_permitted_parameters, if: :devise_controller? 6 7 private 8 9 def configure_permitted_parameters 10 devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :last_name, :first_name, :introduction]) 11 end 12end

ruby

1routes.rb 2 3Rails.application.routes.draw do 4 devise_for :users, controllers: { 5 registrations: 'users/registrations' 6 } 7 resources :blogs 8 9 root to: 'tips#index' 10 resources :users, only: [:show,:destroy] 11 resources :tips do 12 collection do 13 get 'search' 14 get 'tagsearch' 15 get 'detail_search' 16 get 'trend' 17 end 18 resources :comments, only: :create 19 resources :likes, only: [:create, :destroy] 20 end 21end

□仮説及び調べたこと

authenticate_user!を用いて、ログインしていなければログイン画面に移動するということを意図して、registration_controllerとapplication_controllerに記述しましたが、リダイレクトは実行されませんでした。

ユーザー編集機能につきましては以下を参考にさせていただきました。

https://qiita.com/machamp/items/f6a7b003fcda3f04094a

deviseについて理解が甘いので、根本的に間違っているかもしれませんが、ご意見いただけるとありがたいです。

###追記
挙動は以下のようになります。

https://gyazo.com/8d4321ac043f2c14eb5bc448cc73ebce

ログインしていない状態でURLからユーザー編集ページにアクセスすると、
最初はよく分からないページが表示されます。
再度入り直すと、編集ページに入れてしまいます。

ルーティングの表示

rails routes Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel new_user_registration GET /users/sign_up(.:format) users/registrations#new edit_user_registration GET /users/edit(.:format) users/registrations#edit user_registration PATCH /users(.:format) users/registrations#update PUT /users(.:format) users/registrations#update DELETE /users(.:format) users/registrations#destroy POST /users(.:format) users/registrations#create blogs GET /blogs(.:format) blogs#index POST /blogs(.:format) blogs#create new_blog GET /blogs/new(.:format) blogs#new edit_blog GET /blogs/:id/edit(.:format) blogs#edit blog GET /blogs/:id(.:format) blogs#show PATCH /blogs/:id(.:format) blogs#update PUT /blogs/:id(.:format) blogs#update DELETE /blogs/:id(.:format) blogs#destroy root GET / tips#index user GET /users/:id(.:format) users#show DELETE /users/:id(.:format) users#destroy search_tips GET /tips/search(.:format) tips#search tagsearch_tips GET /tips/tagsearch(.:format) tips#tagsearch detail_search_tips GET /tips/detail_search(.:format) tips#detail_search trend_tips GET /tips/trend(.:format) tips#trend tip_comments POST /tips/:tip_id/comments(.:format) comments#create tip_likes POST /tips/:tip_id/likes(.:format) likes#create tip_like DELETE /tips/:tip_id/likes/:id(.:format) likes#destroy tips GET /tips(.:format) tips#index POST /tips(.:format) tips#create new_tip GET /tips/new(.:format) tips#new edit_tip GET /tips/:id/edit(.:format) tips#edit tip GET /tips/:id(.:format) tips#show PATCH /tips/:id(.:format) tips#update PUT /tips/:id(.:format) tips#update DELETE /tips/:id(.:format) tips#destroy

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

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

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

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

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

maisumakun

2021/06/07 00:04

> リダイレクトは実行されませんでした。 「そのまま元のページが表示される」のでしょうか?何かしらのエラーが発生するのでしょうか?
denisov

2021/06/08 13:04 編集

挙動の説明が不十分でした。申し訳ございません。
asm

2021/06/08 14:26

ブラウザのキャッシュを削除しても同様でしょうか? /users/edit.2の.2って多分不要だと思いますがどうでしょう?
denisov

2021/06/08 22:20 編集

確かに、/users/editではリダイレクトが行われます。 投稿欄にrails routesの値を載せました。 しかし、実際、ログインした状態でユーザー編集ページに行きますと、/users/edit.2という数字を含めた表記となります。 editの後の数字がuserのidを表していると認識しています。
denisov

2021/07/18 23:45

回答遅くなってしまい、申し訳ござません。 /users/edit.2の.2って多分不要だと思いますがどうでしょう? →不要でした。当該部分が原因でリダイレクトに不具合が生じていたようです。 私の勉強不足でした。ご指摘ありがとうございます。 また、回答が遅くなってしなった件、重ねてお詫び申し上げます。
guest

回答1

0

ベストアンサー

まず、Devise::RegistrationsControllerは初期状態で「ログインしていないユーザーをログインページにリダイレクトする記述を」されています。

なので、何をすべきか?というと余計に付け加えたbefore_action :authenticate_user!を外すのみです。
(wardenのfailure_appを弄っている場合、そちらも修正する必要がありますが)


ところで、users/registrations#editは「自分を」編集するためのアクションであり、他人の編集はできません。

edit_user_registration GET /users/edit(.:format) users/registrations#edit

なので、2はparams[:format]として解釈されます。
その結果文字化けし、戻る進むボタンで以前読み込んだキャッシュが表示されているような気がします。
どっかのviewでedit_user_registration_path(user)としているのでしょう。

投稿2021/06/09 00:23

asm

総合スコア15147

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問