我正在建立一个包含分层管理员权限的博客。当我使用我们的种子管理员帐户登录时,我可以看到索引就好了。当我转到未登录的页面时,应该由before_action
过滤的内容我收到错误,说明未定义的方法`admin =' for nil:我的索引视图中的NilClass。
当我没有登录时,current_user应为nil,视图中的if语句应解析为false。代码片段供参考,如下所示。
以下是观点:
<div id="blog">
<%= render 'blogs/blog_header' %>
<div class="messages">
<%= render 'layouts/flash_messages' %>
</div>
<%= will_paginate @bloggers %>
<% @bloggers.each do |blogger| %>
<div class="post_wrapper">
<h2 class="title"><%= blogger.name %></h2>
<p class="date_and_author" style="display:inline"><%= blogger.email %></p>
<% if current_blogger.admin = 1 %>
<p class="date_and_author" style="display:inline">||</p>
<button class="button" style="display:inline"><%= link_to "delete", blogger, method: :delete,
data: { confirm: "You sure?" } %></button>
<% end %>
</div>
<% end %>
<%= will_paginate %>
控制器:
class BloggersController < ApplicationController
before_action :signed_in_blogger, only: [:show, :edit, :update, :destroy]
# GET /bloggers
# GET /bloggers.json
def index
@bloggers = Blogger.paginate(page: params[:page], per_page: 20)
end
# GET /bloggers/1
# GET /bloggers/1.json
def show
@blogger = Blogger.find(params[:id])
end
# GET /bloggers/new
def new
@blogger = Blogger.new
end
# GET /bloggers/1/edit
def edit
end
# POST /bloggers
# POST /bloggers.json
def create
@blogger = Blogger.new(blogger_params)
if @blogger.save
blog_sign_in @Blogger
flash.now[:success] = "New account successfully created."
redirect_to "/blogs"
else
render 'new'
end
end
# PATCH/PUT /bloggers/1
# PATCH/PUT /bloggers/1.json
def update
respond_to do |format|
if @blogger.update(blogger_params)
format.html { redirect_to @blogger, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @blogger }
else
format.html { render :edit }
format.json { render json: @blogger.errors, status: :unprocessable_entity }
end
end
end
# DELETE /bloggers/1
# DELETE /bloggers/1.json
def destroy
Blogger.find(params[:id]).destroy
respond_to do |format|
format.html { redirect_to bloggers_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def blogger_params
params.require(:blogger).permit(:name, :email, :password, :admin, :password_confirmation)
end
# Confirms a signed-in user
def signed_in_blogger
unless blog_signed_in?
flash[:error] = "Please sign in."
redirect_to blog_signin_path
end
end
end
帮助者:
# Logs in the given blogger
def blog_sign_in(blogger)
session[:blogger_id] = blogger.id
end
# Returns the current logged-in blogger (if any)
def current_blogger
@current_blogger ||= Blogger.find_by(id: session[:blogger_id])
end
# Returns true if the blogger is logged in, false otherwise.
def blog_signed_in?
!current_blogger.nil?
end
def blog_sign_out
session[:blogger_id] = nil
@current_blogger = nil
end
这可能与某个单独的错误有关,在这个错误中,当我尝试创建一个新的博客时,在点击创建之后,我得到了nil的未定义方法ID:Class?&#34;任何帮助将不胜感激。
根据请求粘贴博客模型:
class Blogger < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :admin, :remember_token
has_secure_password
before_save { self.email = email.downcase }
before_create :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: true
validates :password, length: { minimum: 5 }
validates :password_confirmation, presence: true
def Blogger.new_remember_token
SecureRandom.urlsafe_base64
end
def Blogger.digest(token)
Digest::SHA1.hexdigest(token.to_s)
end
private
def create_remember_token
self.remember_token = Blogger.digest(Blogger.new_remember_token)
end
end
答案 0 :(得分:3)
如果您没有登录,而current_blogger
正如您所说的那样nil
,则admin
将不是可用的方法。要避免此错误,请将视图中的if语句更改为此类。
<% if current_blogger && current_blogger.admin == 1 %>
您还可以使用try
方法,该方法也可以使用
<% if current_blogger.try(:admin) == 1 %>