Ruby on rails 轨道3限制进入
我已经用会话创建了管理员控制器,现在我尝试限制对某些页面的访问 这是我的应用程序_controller.rbRuby on rails 轨道3限制进入,ruby-on-rails,ruby-on-rails-3,authentication,Ruby On Rails,Ruby On Rails 3,Authentication,我已经用会话创建了管理员控制器,现在我尝试限制对某些页面的访问 这是我的应用程序_controller.rb class ApplicationController < ActionController::Base before_filter :authorize, :except => :login protect_from_forgery protected def authorize unless User.find_by_id(session[:user_id]) fl
class ApplicationController < ActionController::Base
before_filter :authorize, :except => :login
protect_from_forgery
protected
def authorize
unless User.find_by_id(session[:user_id])
flash[:notice] = "Please log in"
redirect_to :controller => 'admin', :action => 'login'
end
end
end
class ApplicationController:登录
保护你不被伪造
受保护的
def授权
除非User.find\u by\u id(会话[:User\u id])
flash[:notice]=“请登录”
将_重定向到:controller=>“admin”,:action=>“login”
结束
结束
结束
这是我试图限制其访问权限的控制器
class PhotosController < ApplicationController
def index
@photos = Photo.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @photos }
end
end
def show
@photo = Photo.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @photo }
end
end
def new
@photo = Photo.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @photo }
end
end
def edit
@photo = Photo.find(params[:id])
end
def create
upload = params[:upload]
@photo = Photo.new(params[:photo])
respond_to do |format|
if @photo.save
post = Datafile.save(upload, @photo.id)
format.html { redirect_to(@photo, :notice => 'Photo was successfully created.') }
format.xml { render :xml => @photo, :status => :created, :location => @photo }
else
format.html { render :action => "new" }
format.xml { render :xml => @photo.errors, :status => :unprocessable_entity }
end
end
end
def update
@photo = Photo.find(params[:id])
respond_to do |format|
if @photo.update_attributes(params[:photo])
format.html { redirect_to(@photo, :notice => 'Photo was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @photo.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@photo = Photo.find(params[:id])
@photo.destroy
respond_to do |format|
post = Datafile.delete(@photo.id)
format.html { redirect_to(photos_url) }
format.xml { head :ok }
end
end
protected
def authorize
end
end
class photocontroller@photos}
结束
结束
def秀
@photo=photo.find(参数[:id])
回应待办事项|格式|
format.html#show.html.erb
format.xml{render:xml=>@photo}
结束
结束
def新
@photo=photo.new
回应待办事项|格式|
format.html#new.html.erb
format.xml{render:xml=>@photo}
结束
结束
定义编辑
@photo=photo.find(参数[:id])
结束
def创建
upload=params[:upload]
@照片=照片。新建(参数[:照片])
回应待办事项|格式|
如果@photo.save
post=Datafile.save(上传@photo.id)
format.html{redirect_to(@photo,:notice=>“照片已成功创建”)}
format.xml{render:xml=>@photo,:status=>:created,:location=>@photo}
其他的
format.html{render:action=>“new”}
format.xml{render:xml=>@photo.errors,:status=>:unprocessable_entity}
结束
结束
结束
def更新
@photo=photo.find(参数[:id])
回应待办事项|格式|
if@photo.update_属性(参数[:photo])
format.html{redirect_to(@photo,:notice=>“照片已成功更新”)}
format.xml{head:ok}
其他的
format.html{render:action=>“edit”}
format.xml{render:xml=>@photo.errors,:status=>:unprocessable_entity}
结束
结束
结束
def销毁
@photo=photo.find(参数[:id])
@照片。销毁
回应待办事项|格式|
post=Datafile.delete(@photo.id)
format.html{重定向到(照片url)}
format.xml{head:ok}
结束
结束
受保护的
def授权
结束
结束
但它不会限制它,可以告诉我我缺少什么吗?从应用程序控制器交换线路
before_filter :authorize, :except => :login
before_filter :authorize
用这个
helper_method :authorize
并将此行粘贴到照片控制器中
before_filter :authorize, :except => :login
before_filter :authorize
从应用程序控制器交换线路
before_filter :authorize, :except => :login
before_filter :authorize
用这个
helper_method :authorize
并将此行粘贴到照片控制器中
before_filter :authorize, :except => :login
before_filter :authorize
authorize
方法不起作用,因为您在PhotosController
底部将其重新定义为空白方法。删除此重新定义,然后它将工作。由于您在PhotosController
底部将其重新定义为空白方法,因此授权
方法不起作用。删除此重新定义,然后它将工作。helper\u方法
使此方法在视图中可用。这不会解决问题。helper\u方法
使此方法在视图中可用。这并不能解决那个问题。