未定义的方法`admin?'为#

时间:2014-12-20 22:48:25

标签: ruby-on-rails

下午好,

我正在尝试修复我的rails应用程序。

这是该问题的直接形象:

http://oi62.tinypic.com/2z6v5w2.jpg

这是我的topic_policy.rb

class TopicPolicy < ApplicationPolicy

def index?
true
end

def create?
user.present? && user.admin?
end

def update?
create?
end
end

这是我的application_policy.rb

class ApplicationPolicy
attr_reader :user, :record

def initialize(user, record)
@user = user
@record = record
end

def index?
false
end

def show?
scope.where(:id => record.id).exists?
end

def create?
user.present?
end

def new?
create?
end

def update?
user.present? && (record.user == user || user.admin?)
end

def edit?
update?
end

def destroy?
update?
end

def scope
record.class
end

class Scope
attr_reader :user, :scope

def initialize(user, scope)
@user = user
@scope = scope
end

def resolve
scope
end
end
end

这是topics_controller.rb

class TopicsController < ApplicationController
def index
@topics = Topic.all
authorize @topics
end

def new
@topic = Topic.new
authorize @topic
end

def show
@topic = Topic.find(params[:id])
authorize @topic
end

def edit
@topic = Topic.find(params[:id])
authorize @topic
end

def create
@topic = Topic.new(params.require(:topic).permit(:name, :description, :public))
authorize @topic
if @topic.save
redirect_to @topic, notice: "Topic was saved successfully."
else
flash[:error] = "Error creating topic. Please try again."
render :new
end
end

def update
@topic = Topic.find(params[:id])
authorize @topic
if @topic.update_attributes(params.require(:topic).permit(:name, :description, :public))
redirect_to @topic
else
flash[:error] = "Error saving topic. Please try again"
render :edit
end
end
end

我只是试图让它通过我的rails应用程序查看网页,但我被卡住了。

1 个答案:

答案 0 :(得分:0)

我能够通过将admin方法设为public来解决此问题,将其设置为private。