私は最初のRailsアプリケーションを構築しており、ヘルパーインスタンス変数をRailsAdmin.configで使用できるようにする方法を知らない。現在ログインしているユーザーに管理者権限がある場合にのみ、rails adminへのアクセスを許可したいと思います。私はRailsAdmin.configコードがどこに行かなければならないのか分かりませんが、現在はenvironment.rbにあります。私がログインすると、未定義のローカル変数またはメソッドエラーがcurrent_userになっています。助けてくれてありがとう。Rails管理 - セッションヘルパー変数を設定できるようにする
environment.rbに
RailsAdmin.config do |config|
config.authorize_with do
redirect_to main_app.root_path unless current_user.admin?
end
end
sessions_helper.rb
module SessionsHelper
def login(user)
session[:user_id] = user.id
end
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
end
end
end
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
end
感謝を参照してください。 2番目の方法が働いた。最初のメソッドはこのエラーを返します。 ' ':クラスApplicationController(TypeError)のスーパークラスの不一致です。 –
neridaj