更新用户时绕过批量分配时出现名称错误?
我按照 Railscast 教程绕过批量分配来编辑我的 <我的 User
模型的 code>role 属性为 “admin”
。这就是我定义角色的方式:
class User < ActiveRecord::Base
attr_accessible :email, :password, :remember_me
attr_accessor :accessible
devise :database_authenticatable, ....etc
before_create :setup_default_role_for_new_users
ROLES = %w[admin default banned]
private
def setup_default_role_for_new_users
if self.role.blank?
self.role = "default"
end
end
def mass_assignment_authorizer
super + (accessible || [])
end
end
然后我创建了一个新的 UsersController
只是为了我的更新方法出现问题:
def update
@user = User.find(params[:id])
@user.accessible = [:role] if user.role == "admin"
if @user.update_attributes(params[:user])
redirect_to @user, :notice => "Successfully updated user."
else
render :action => 'edit'
end
end
但我不能这样做,因为这一行: if user.role = =“admin”
,导致问题,给我错误:
NameError (undefined local variable or method `user' for UsersController
我在这里缺少什么?
提前致谢。
I followed the Railscast tutorial for bypassing mass assignment to edit my role
attribute of my User
model as the "admin"
. This is how I defined my roles:
class User < ActiveRecord::Base
attr_accessible :email, :password, :remember_me
attr_accessor :accessible
devise :database_authenticatable, ....etc
before_create :setup_default_role_for_new_users
ROLES = %w[admin default banned]
private
def setup_default_role_for_new_users
if self.role.blank?
self.role = "default"
end
end
def mass_assignment_authorizer
super + (accessible || [])
end
end
And then I created a new UsersController
only to have issues with my update method:
def update
@user = User.find(params[:id])
@user.accessible = [:role] if user.role == "admin"
if @user.update_attributes(params[:user])
redirect_to @user, :notice => "Successfully updated user."
else
render :action => 'edit'
end
end
I can't do this though becuase this line: if user.role == "admin"
, is causing issue, giving me the error:
NameError (undefined local variable or method `user' for UsersController
What am I missing here?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过
user.role == "admin"
中的user
部分,您尝试使用本地变量,该变量尚未在update< 中定义/代码> 方法。如果
user
没有声明为可在控制器中访问的辅助方法,那么 ruby 将找不到它。从您的代码中我假设只有管理员用户可以更新另一个用户的角色?因此,您没有使用
@user.role == "admin"
而是使用user.role == "admin"
?如果是这样,您必须提供一个
user
对象,无论是通过辅助方法(即在您的 ApplicationHelper 类中)还是在尝试在 update 方法中使用它之前获取它,或者在 update 方法中使用 before_* 回调你的控制器。我希望我的意思很清楚。
With the
user
part inuser.role == "admin"
you're trying to use a local variable, which hasn't been defined in yourupdate
method. Ifuser
isn't declared as a helper method that's accessible in your controllers then ruby won't find it.From your code I'm assuming that only an admin user can update the role of another user? Thus you're not using
@user.role == "admin"
butuser.role == "admin"
?If so you have to provide a
user
object whether it's through a helper method (i.e. in your ApplicationHelper class) or fetch it before you try to use it in your update method, or with a before_* callback in your controller.I hope it's clear what I meant.