Ruby on rails admincontroller使用错误的帮助程序进行创建操作
我试图通过我的admins controller创建操作创建一个admin实例,但我不断收到一个错误,该错误显示:Ruby on rails admincontroller使用错误的帮助程序进行创建操作,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我试图通过我的admins controller创建操作创建一个admin实例,但我不断收到一个错误,该错误显示: ActiveRecord::RecordNotFound in AdminsController\show:找不到id=4的用户 跟踪表明它正在尝试使用会话帮助程序(对于用户)而不是相应的adminsessions帮助程序 app/helpers/sessions_helper.rb:20:in `current_user' app/helpers/sessions_helper.
ActiveRecord::RecordNotFound in AdminsController\show:找不到id=4的用户
跟踪表明它正在尝试使用会话帮助程序(对于用户)而不是相应的adminsessions帮助程序
app/helpers/sessions_helper.rb:20:in `current_user'
app/helpers/sessions_helper.rb:12:in `signed_in?'
app/views/layouts/application.html.erb:13:in
app_views_layouts_application_html_erb__1013605049_93953830
我可以正确登录并创建管理员。我只是认为问题与我的管理员控制器中的重定向到@admin
有关,尽管我不确定
如何设置它,使我的管理员控制器使用adminsessions助手而不是sessions助手?任何帮助都将不胜感激
adminsessions\u controller.rb
class AdminsessionsController < ApplicationController
def new
@title = "Log in"
end
def show
@title = "Admin session"
end
def create
admin = Admin.authenticate(params[:adminsession][:email],
params[:adminsession][:password])
if admin.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Log in"
render 'new'
else
sign_in admin
redirect_to admin
end
end
def destroy
sign_out
redirect_to root_path
end
end
class AdminsController < ApplicationController
def index
@user = User.all
end
def show
@admin = Admin.find(params[:id])
end
def new
@admin = Admin.new
@title = "New admin"
end
def create
@admin = Admin.new(params[:admin])
if @admin.save
sign_in @admin
flash[:success] = "Welcome admin!"
redirect_to @admin
else
@title = "New admin"
render 'new'
end
end
end
adminsessions\u helper.rb
module SessionsHelper
def sign_in(user)
session[:user_id] = user.id
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def current_user?(user)
user == current_user
end
def authenticate
deny_access unless signed_in?
end
def sign_out
session[:user_id] = nil
self.current_user = nil
end
def redirect_back_or(default)
redirect_to(session[:return_to] || default)
clear_return_to
end
def deny_access
store_location
redirect_to login_path, :notice => "Please log in to access this page."
end
private
def store_location
session[:return_to] = request.fullpath
end
def clear_return_to
session[:return_to] = nil
end
end
module AdminsessionsHelper
def sign_in(admin)
adminsession[:admin_id] = admin.id
self.current_admin = admin
end
def signed_in?
!current_admin.nil?
end
def current_admin=(admin)
@current_admin = admin
end
def current_admin
@current_admin ||= Admin.find(adminsession[:admin_id]) if adminsession[:admin_id]
end
def current_admin?(admin)
admin == current_admin
end
def authenticate
deny_access unless signed_in?
end
def sign_out
adminsession[:admin_id] = nil
self.current_admin = nil
end
def redirect_back_or(default)
redirect_to(adminsession[:return_to] || default)
clear_return_to
end
def deny_access
store_location
redirect_to login_path, :notice => "Please log in to access this page."
end
private
def store_location
adminsession[:return_to] = request.fullpath
end
def clear_return_to
adminsession[:return_to] = nil
end
end
所有辅助对象(默认情况下)在所有控制器中混合使用并可用。看起来您正在使用的方法应该是受保护的,或者是控制器的私有成员。您可以使它们成为视图中可用的辅助方法,即helper\u方法:signed\u-in?
就我个人而言,我从来都不喜欢与助手之间缺少名称空间。我更喜欢演示者模式(请参阅)。谢谢你,aceofspades。重命名方法以使其唯一解决了问题。我不应该假设每个助手都特定于其控制器。如果你是Rails@gemmy的新手,同样,这些方法应该是受保护/私有控制器方法。在Rails中,任何东西都有一个位置,学习Rails意味着学习哪里一切都属于你,很高兴这有帮助。
module AdminsessionsHelper
def sign_in(admin)
adminsession[:admin_id] = admin.id
self.current_admin = admin
end
def signed_in?
!current_admin.nil?
end
def current_admin=(admin)
@current_admin = admin
end
def current_admin
@current_admin ||= Admin.find(adminsession[:admin_id]) if adminsession[:admin_id]
end
def current_admin?(admin)
admin == current_admin
end
def authenticate
deny_access unless signed_in?
end
def sign_out
adminsession[:admin_id] = nil
self.current_admin = nil
end
def redirect_back_or(default)
redirect_to(adminsession[:return_to] || default)
clear_return_to
end
def deny_access
store_location
redirect_to login_path, :notice => "Please log in to access this page."
end
private
def store_location
adminsession[:return_to] = request.fullpath
end
def clear_return_to
adminsession[:return_to] = nil
end
end