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

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

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

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

Ruby

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

Ruby on Rails 6

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

Q&A

解決済

1回答

2478閲覧

RailsでコントローラーからApplicationControllerのメソッドにアクセスできない

退会済みユーザー

退会済みユーザー

総合スコア0

Devise

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

Ruby

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

Ruby on Rails 6

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

0グッド

0クリップ

投稿2021/01/03 04:13

編集2021/01/03 04:26

RailsをAPIとして使っています。devise token authでトークン認証を導入し、ログイン認証は問題なくできています。しかし、リソースにアクセスしようとすると、ApplicationControllerに存在するメソッドにサブコントローラーからアクセスできず、NoMethodErrorとなってしまいます。
skip_before_action :verify_authenticity_tokenをコメントアウトするしないで、ログインができるできない、リソースにアクセスできるできないが変わってくるようなのですが、どう対処したら良いのでしょうか。

ruby 2.6.6
rails 6.0.3.4
devise 4.7.3
devise_token_auth 1.1.3

Ruby

1エラーメッセージ 2NoMethodError in SubController#index 3undefined method `methodA' for #<SubController:0x0000559c2a82e8c8> 4 5Extracted source (around line #428): 6 7 lambda do |target, value, &block| 8 target, block, method, *arguments = expand(target, value, block) 9 target.send(method, *arguments, &block) 10 end 11end 12

Ruby

1class ApplicationController < ActionController::API 2 include DeviseTokenAuth::Concerns::SetUserByToken 3 skip_before_action :verify_authenticity_token #コメントアウトすると、リソースにアクセスできるが、ログインできない 4 before_action :authenticate_api_user! 5 6 protected 7 def methodA 8 # 略 9 end 10end

Ruby

1class SubController < ApplicationController 2 before_action :methodA, only: [:index] 3 4 def index 5 # 略 6 end 7 8end

Ruby

1DeviseTokenAuth.setup do |config| 2 # By default the authorization headers will change after each request. The 3 # client is responsible for keeping track of the changing tokens. Change 4 # this to false to prevent the Authorization header from changing after 5 # each request. 6 config.change_headers_on_each_request = false 7 8 # By default, users will need to re-authenticate after 2 weeks. This setting 9 # determines how long tokens will remain valid after they are issued. 10 config.token_lifespan = 2.weeks 11 12 # Limiting the token_cost to just 4 in testing will increase the performance of 13 # your test suite dramatically. The possible cost value is within range from 4 14 # to 31. It is recommended to not use a value more than 10 in other environments. 15 config.token_cost = Rails.env.test? ? 4 : 10 16 17 # Sets the max number of concurrent devices per user, which is 10 by default. 18 # After this limit is reached, the oldest tokens will be removed. 19 # config.max_number_of_devices = 10 20 21 # Sometimes it's necessary to make several requests to the API at the same 22 # time. In this case, each request in the batch will need to share the same 23 # auth token. This setting determines how far apart the requests can be while 24 # still using the same auth token. 25 # config.batch_request_buffer_throttle = 5.seconds 26 27 # This route will be the prefix for all oauth2 redirect callbacks. For 28 # example, using the default '/omniauth', the github oauth2 provider will 29 # redirect successful authentications to '/omniauth/github/callback' 30 # config.omniauth_prefix = "/omniauth" 31 32 # By default sending current password is not needed for the password update. 33 # Uncomment to enforce current_password param to be checked before all 34 # attribute updates. Set it to :password if you want it to be checked only if 35 # password is updated. 36 # config.check_current_password_before_update = :attributes 37 38 # By default we will use callbacks for single omniauth. 39 # It depends on fields like email, provider and uid. 40 # config.default_callbacks = true 41 42 # Makes it possible to change the headers names 43 config.headers_names = {:'access-token' => 'access-token', 44 :'client' => 'client', 45 :'expiry' => 'expiry', 46 :'uid' => 'uid', 47 :'token-type' => 'token-type' } 48 49 # By default, only Bearer Token authentication is implemented out of the box. 50 # If, however, you wish to integrate with legacy Devise authentication, you can 51 # do so by enabling this flag. NOTE: This feature is highly experimental! 52 # config.enable_standard_devise_support = false 53end 54

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

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

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

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

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

maisumakun

2021/01/03 04:18

> ApplicationControllerに存在するメソッドにサブコントローラーからアクセスできず、NoMethodErrorとなってしまいます。 エラーメッセージを「そのまま」書いていただけないでしょうか?
退会済みユーザー

退会済みユーザー

2021/01/03 04:27

ChromeのNetworkから確認できたエラーメッセージを追加しました。
退会済みユーザー

退会済みユーザー

2021/01/03 04:28

ちなみに、methodAをサブクラス側に記述すると、問題なくレスポンスが帰ってきます。
guest

回答1

0

ベストアンサー

before_action :authenticate_api_user!をサブクラス側に移動させて対応しました。
どうやら、 skip_before_action :verify_authenticity_tokenと一緒にスーパークラスに記載していた点で相性が悪かったようです。

投稿2021/01/13 02:58

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問