Rails 传递给 class 后活动记录为空
Rails Active record null after passed to class
我希望能够将我的用户对象传递给另一个 class 进行验证。基本上我会做类似
的事情
我的控制器:
def new
user = User.find(1)
logger.info "#{user.id}, #{user.name}, #{user.isadmin}"
#The above is logged with 1, test, true
uhelper = UserHelper.new(user)
if !uhelper.isAdmin
#Only admins can access this page
redirect_to root_path
end
end
在app/models
Class UserHelper
def initialize(user)
@user = user
end
def isAdmin
if @user.isadmin
true
end
nil
end
控制器中的 if 语句总是解析为 nil,即使我知道记录是正确的。我不能像这样正确地将 ActiveRecords 传递给 classes 吗?
有人知道为什么会发生这种情况吗?
编辑
undefined method `isadmin' for nil:NilClass
app/models/userfnc.rb:14:in `isAdmin'
app/controllers/rosters_controller.rb:12:in `index'
sqlite> select * from users;
1|testuser|test@test.com|20170601|20170601|1
sqlite> .schema users
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar DEFAULT NULL, "email" varchar DEFAULT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "isadmin" boolean);
您可以对代码进行相当多的代码改进。
关于您的问题,可能是您的机壳问题。检查你的 table。您的字段可能被称为 isadmin
而不是 isAdmin
。
关于您的代码改进,以下内容可以帮助您:
def isAdmin
if @user.isAdmin
true
end
nil
end
你可以用一行来完成:
def isAdmin
@user.isAdmin
end
这个位,
uhelper = UserHelper.new(user)
if !uhelper.isAdmin
#Only admins can access this page
redirect_to root_path
end
当您有一个像这样的简单表达式时,有时将其缩减为一行会更容易:
uhelper = UserHelper.new(user)
redirect_to root_path unless uhelper.isAdmin
但是... Rails 标准是在这种情况下使用过滤器。把它放在过滤器方法中,而不是那一点。
class MyController
before_filter :check_admin
...
...
private
def check_admin
redirect_to root_path unless user.isAdmin
end
end
我希望能够将我的用户对象传递给另一个 class 进行验证。基本上我会做类似
的事情我的控制器:
def new
user = User.find(1)
logger.info "#{user.id}, #{user.name}, #{user.isadmin}"
#The above is logged with 1, test, true
uhelper = UserHelper.new(user)
if !uhelper.isAdmin
#Only admins can access this page
redirect_to root_path
end
end
在app/models
Class UserHelper
def initialize(user)
@user = user
end
def isAdmin
if @user.isadmin
true
end
nil
end
控制器中的 if 语句总是解析为 nil,即使我知道记录是正确的。我不能像这样正确地将 ActiveRecords 传递给 classes 吗?
有人知道为什么会发生这种情况吗?
编辑
undefined method `isadmin' for nil:NilClass
app/models/userfnc.rb:14:in `isAdmin'
app/controllers/rosters_controller.rb:12:in `index'
sqlite> select * from users;
1|testuser|test@test.com|20170601|20170601|1
sqlite> .schema users
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar DEFAULT NULL, "email" varchar DEFAULT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "isadmin" boolean);
您可以对代码进行相当多的代码改进。
关于您的问题,可能是您的机壳问题。检查你的 table。您的字段可能被称为 isadmin
而不是 isAdmin
。
关于您的代码改进,以下内容可以帮助您:
def isAdmin
if @user.isAdmin
true
end
nil
end
你可以用一行来完成:
def isAdmin
@user.isAdmin
end
这个位,
uhelper = UserHelper.new(user)
if !uhelper.isAdmin
#Only admins can access this page
redirect_to root_path
end
当您有一个像这样的简单表达式时,有时将其缩减为一行会更容易:
uhelper = UserHelper.new(user)
redirect_to root_path unless uhelper.isAdmin
但是... Rails 标准是在这种情况下使用过滤器。把它放在过滤器方法中,而不是那一点。
class MyController
before_filter :check_admin
...
...
private
def check_admin
redirect_to root_path unless user.isAdmin
end
end