问题描述
我跟着&安培认证;已经功成名就wworking罚款。现在,我想只显示编辑和放大器;摧毁我的索引页admin用户的链接。
我已经建立了万亩用户数据库与管理布尔字段和放大器;试图把一个简单的if语句中的另一个模型(hikingtrails)的观点仅显示某些环节admin用户,但是当我尝试一下,未定义的方法我得到这个错误管理员?对于零:NilClass
数据库架构
CREATE_TABLE用户:力=>真正做| T |
t.string电子邮件
t.stringpassword_digest
t.boolean管理员
t.datetimecreated_at:空=>假
t.datetime的updated_at:空=>假
结束
用户模型
类用户< ActiveRecord的::基地
attr_accessible:电子邮件,:密码:password_confirmation#:管理员 验证:电子邮件:独特= GT;真正 has_secure_password
结束
应用程序控制器
类的ApplicationController< ActionController的基地::
protect_from_forgery #获取当前登录的用户记录是否用户在当前登录
#投入的ApplicationController此方法,以便它在所有控制器可用
私人的
高清CURRENT_USER
#检查基于所述会话的用户ID的用户时,他们中记录被存储,并且存储导致一个实例变量
@current_user || = User.find(会话[:USER_ID])如果会话[:USER_ID]
结束
#给访问这个方法从所有的意见,是helper_method使得一个辅助方法
是helper_method:CURRENT_USER #基本授权,用户必须登录!
DEF授权
redirect_to的LOGIN_URL,警惕:你必须先登录才能执行此操作,如果current_user.nil?
结束
结束
的意见/ hikingtrails / index.html.erb
<如果current_user.admin%? %GT;
<%=的link_to T('编辑',:默认=> T(helpers.links.edit)),
edit_hikingtrail_path(hikingtrail):类=> BTN BTN-迷你'%GT;
<%=的link_to T('破坏',:默认=> T(helpers.links.destroy)),
hikingtrail_path(hikingtrail)
:方法=> :删除,
:数据=> {:确认=> T('确认',:默认=> T(helpers.links.confirm:默认=>'?你确定'))},
:类=> BTN BTN-迷你BTN-危险%GT;
<%结束%GT;
CURRENT_USER
将是零,如果用户没有按照您的code记录。所以,你需要做到这一点:
<%如果CURRENT_USER和放大器;&安培; current_user.admin? %GT;
或使用尝试
方法Rails添加到所有对象。
<%如果current_user.try(?:管理员)%GT;
I followed railscast #250 Authentication from Scratch & got everthing wworking fine. Now I'm trying to only display edit & destroy links on my index page to admin user's.
I've set up mu User database with a admin boolean field & tried putting a simple if statement in the view of another model (hikingtrails) to only display certain links to admin users but I get this error when I try it out, undefined method 'admin?' for nil:NilClass
Database Schema
create_table "users", :force => true do |t|
t.string "email"
t.string "password_digest"
t.boolean "admin"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
User Model
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation#, :admin
validates :email, :uniqueness => true
has_secure_password
end
Application Controller
class ApplicationController < ActionController::Base
protect_from_forgery
# fetch the currently logged-in user record to see if the user is currently logged in
# putting this method in ApplicationController so that it’s available in all controllers
private
def current_user
# checks for a User based on the session’s user id that was stored when they logged in, and stores result in an instance variable
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
# to give access to this method from all the views, the helper_method makes it a helper method
helper_method :current_user
# basic authorization, user must be logged in!
def authorize
redirect_to login_url, alert: "You must be logged in to perform this action" if current_user.nil?
end
end
views/hikingtrails/index.html.erb
<% if current_user.admin? %>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_hikingtrail_path(hikingtrail), :class => 'btn btn-mini' %>
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
hikingtrail_path(hikingtrail),
:method => :delete,
:data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
:class => 'btn btn-mini btn-danger' %>
<% end %>
current_user
will be nil if a user is not logged in according to your code. So you need to do this:
<% if current_user && current_user.admin? %>
or using the try
method Rails adds to all objects.
<% if current_user.try(:admin?) %>
这篇关于未定义的方法`管理?对于零:NilClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!