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

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

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

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

Q&A

解決済

1回答

1376閲覧

rails_adminでundefined method `new' for nil:NilClass

yamada_yuuki

総合スコア100

Ruby on Rails

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

0グッド

1クリップ

投稿2019/11/27 08:37

rails_adminで管理者画面を作りました。最初は上手く行ったのですが5回目くらいのときに以下のエラーが出て困っています。原因・解決方法がわかる方は教えていただけると幸いです。https://qiita.com/mailok1212/items/8e36a5a10f19023bb0c4←参考にしたサイト

error

1Started GET "/admin" for ::1 at 2019-11-27 17:23:30 +0900 2 (19.4ms) SELECT sqlite_version(*) 3 (2.0ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC 4Processing by RailsAdmin::MainController#dashboard as HTML 5 User Load (1.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 63], ["LIMIT", 1]] 7 ↳ config/initializers/rails_admin.rb:7:in `block (2 levels) in <top (required)>' 8Completed 500 Internal Server Error in 93ms (ActiveRecord: 7.7ms | Allocations: 9176) 9 10 11NoMethodError (undefined method `new' for nil:NilClass):

gem

1(今回使用したもの) 2gem 'devise' 3gem 'rails_admin' 4gem 'rails-i18n' 5gem 'cancancan'

railsadminrb

1RailsAdmin.config do |config| 2 3 ### Popular gems integration 4 5 ## == Devise == 6 config.authenticate_with do 7 warden.authenticate! scope: :user 8 end 9 config.current_user_method(&:current_user) 10 11 ## == Cancan == 12 config.authorize_with :cancan 13 14 ## == Pundit == 15 # config.authorize_with :pundit 16 end

Ability

1# frozen_string_literal: true 2 3class Ability 4 include CanCan::Ability 5 6 def initialize(user) 7 if user.try(:admin?) 8 can :access, :rails_admin 9 can :manage, :all 10 # Define abilities for the passed in user here. For example: 11 # 12 # user ||= User.new # guest user (not logged in) 13 # if user.admin? 14 # can :manage, :all 15 # else 16 # can :read, :all 17 # end 18 # 19 # The first argument to `can` is the action you are giving the user 20 # permission to do. 21 # If you pass :manage it will apply to every action. Other common actions 22 # here are :read, :create, :update and :destroy. 23 # 24 # The second argument is the resource the user can perform the action on. 25 # If you pass :all it will apply to every resource. Otherwise pass a Ruby 26 # class of the resource. 27 # 28 # The third argument is an optional hash of conditions to further filter the 29 # objects. 30 # For example, here the user can only update published articles. 31 # 32 # can :update, Article, :published => true 33 # 34 # See the wiki for details: 35 # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities 36 end 37 end 38end

https://qiita.com/Masahiro_T/items/130d7cda3ea5de08985eこのサイトの方法を試しましたが、you are not authorized to access this page. といわれてしまいます。

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

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

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

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

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

perpouh

2019/12/04 09:46

>you are not authorized to access this page. といわれてしまいます。 cancancanって確か権限周りのgemだったと記憶していますが、開こうとした画面に対してログインしたユーザーの権限が無いのでは? 冒頭のエラーは既に直っていて、別の現象が発生しているように見えますがいかがでしょう?
guest

回答1

0

自己解決

1からadminとcancancanの設定をし直したらできました。原因はわかりません。すみません。

投稿2019/12/24 08:49

yamada_yuuki

総合スコア100

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問