前回、「ActiveAdminで『ArgumentError』エラーに困っています」という質問をこちらでさせていただいたのですが、どうも回答の糸口が掴めなかったため、あきらめてしまい・・・
「active_admin」ではなく、「railsadmin」を使うことにしました。
こちらのQiita記事を参考にしながら、railsadminを実装進めていき、なんとかlocalhost:3000/adminで表示されました!
ここまでは良かったのですが、なぜか「/admin」に行く際にログインを要求されないのです。
rails c で「admin_user」を作成したのにもかかわらず、ログイン画面が表示されず、誰でも/adminに入れるような仕組みになってしまっています。
active_adminで直さなかった、下記のエラーと関係があるのでしょうか・・・
ruby
1ArgumentError in ActiveAdmin::Devise::SessionsController#new 2wrong number of arguments (1 for 2) 3 4 # from the path, or the handler, we should return the array of formats given 5 # to the resolver. 6 def extract_handler_and_format_and_variant(path, default_formats) 7 pieces = File.basename(path).split('.'.freeze) 8 pieces.shift
こちらでDeviseで作成したユーザーをパスワードなしで編集できるようにしてしまったのですが、それが原因となっているのでしょうか・・・
すみませんが、お助けくださいませ・・・
##[追記] gem cancanを追加しました
gem cancan 1.6.10
ご指摘にあったので、cancanをgemに追加してみました。
こちらの記事にあるようにcancan設定を進めていきましたが、下記のようなエラーが生じてしまいました。
Ruby
1CanCan::AccessDenied in RailsAdmin::MainController#dashboard 2 3You are not authorized to access this page. 4 5 if cannot?(action, subject, *args) 6 message ||= unauthorized_message(action, subject) 7 raise AccessDenied.new(message, action, subject) 8 end 9 subject 10 end
このエラー表示のわけはおそらく、ユーザーにadminが付与されていないというものかと思われますが、ログインしているユーザー状態はadminが「true」で付与されているものなんです・・・。(/adminにとぶとエラーが表示されてしまいます。)
##該当するソースコード
rails_admin.rb
ruby
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 17 ## == PaperTrail == 18 # config.audit_with :paper_trail, 'User', 'PaperTrail::Version' # PaperTrail >= 3.0.0 19 20 ### More at https://github.com/sferik/rails_admin/wiki/Base-configuration 21 22 ## == Gravatar integration == 23 ## To disable Gravatar integration in Navigation Bar set to false 24 # config.show_gravatar = true 25 26 config.actions do 27 dashboard # mandatory 28 index # mandatory 29 new 30 export 31 bulk_delete 32 show 33 edit 34 delete 35 show_in_app 36 37 ## With an audit adapter, you can add: 38 # history_index 39 # history_show 40 end 41end
ability.rb
ruby
1class Ability 2 include CanCan::Ability 3 4 def initialize(user) 5 if user && user.admin? 6 can :access, :rails_admin # grant access to rails_admin 7 can :manage, :all # allow superadmins to do anything 8 end 9 end 10end
application.rb
ruby
1class ApplicationController < ActionController::Base 2 protect_from_forgery with: :exception 3 4 include Jpmobile::ViewSelector 5 before_action :set_view_path 6 7 protected 8 9 def configure_permitted_parameters 10 devise_parameter_sanitizer.permit(:sign_up, keys: [:name]) 11 devise_parameter_sanitizer.permit(:account_update, keys: [:name, :background, :image]) 12 end 13 14 class ApplicationController < ActionController::Base 15 rescue_from CanCan::AccessDenied do |exception| 16 redirect_to root_url, :alert => exception.message 17 end 18 end 19 20 private 21 22 def set_view_path 23 path = request.smart_phone? ? 'smartphone' : 'pc' 24 prepend_view_path(Rails.root + 'app/views' + path) 25 end 26end 27
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/21 07:48
2017/06/21 08:00
2017/06/21 08:14
2017/06/21 08:42
2017/06/21 10:07
2017/06/21 11:45
2017/06/21 14:29