Ruby on rails 3 如何将普通用户指定为超级用户或管理员
我在上学习了教程,并对其进行了修改,使未注册的人能够使用用户名、电子邮件和密码注册。Ruby on rails 3 如何将普通用户指定为超级用户或管理员,ruby-on-rails-3,authentication,if-statement,admin,Ruby On Rails 3,Authentication,If Statement,Admin,我在上学习了教程,并对其进行了修改,使未注册的人能够使用用户名、电子邮件和密码注册。 我已经允许登录用户修改其电子邮件和密码,但不能修改用户名 我想补充的是: 1) 要使登录用户能够查看/访问其用户名和电子邮件, 2) 为了使设置了admin_标志的用户(我在sql表中处理了这个问题并创建了用户)能够查看/修改所有用户记录 我修改了app/cotrollers/user_controller.rb如下: class UsersController < ApplicationControll
我已经允许登录用户修改其电子邮件和密码,但不能修改用户名 我想补充的是: 1) 要使登录用户能够查看/访问其用户名和电子邮件,
2) 为了使设置了admin_标志的用户(我在sql表中处理了这个问题并创建了用户)能够查看/修改所有用户记录 我修改了app/cotrollers/user_controller.rb如下:
class UsersController < ApplicationController
before_filter :is_user, :only => [:index, :show, :edit, :update, :destroy]
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
end
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
def new
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user }
end
end
def edit
end
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
flash[:notice] = 'Registration successful.'
format.html { redirect_to(:controller => 'home', :action => 'tutorial') }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @user.update_attributes(params[:user])
flash[:notice] = 'Your profile was successfully updated.'
format.html { redirect_to(:controller => 'home', :action => 'index') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head :ok }
end
end
def is_user
if User.exists?(params[:id])
@user = User.find(params[:id]);
if current_user.admin_flag == true
flash[:notice] = 'Welcome Admin'
end
if !current_user || current_user.id != @user.id
flash[:notice] = 'You do not have access to that page'
redirect_to(:controller => 'home', :action => 'index')
end
else
flash[:notice] = 'You do not have access to that page'
redirect_to(:controller => 'home', :action => 'index')
end
end
end
class UsersController[:index,:show,:edit,:update,:destroy]
def索引
@users=User.all
回应待办事项|格式|
format.html#index.html.erb
format.xml{render:xml=>@users}
终止
终止
def秀
@user=user.find(参数[:id])
回应待办事项|格式|
format.html#show.html.erb
format.xml{render:xml=>@user}
终止
终止
def新
@user=user.new
回应待办事项|格式|
format.html#new.html.erb
format.xml{render:xml=>@user}
终止
终止
定义编辑
终止
def创建
@user=user.new(参数[:user])
回应待办事项|格式|
如果@user.save
flash[:notice]=“注册成功。”
format.html{redirect_to(:controller=>'home',:action=>'tutorial')}
format.xml{render:xml=>@user,:status=>:created,:location=>@user}
其他的
format.html{render:action=>“new”}
format.xml{render:xml=>@user.errors,:status=>:unprocessable_entity}
终止
终止
终止
def更新
回应待办事项|格式|
如果@user.update_属性(参数[:user])
flash[:notice]=“您的个人资料已成功更新。”
format.html{redirect_to(:controller=>'home',:action=>'index')}
format.xml{head:ok}
其他的
format.html{render:action=>“edit”}
format.xml{render:xml=>@user.errors,:status=>:unprocessable_entity}
终止
终止
终止
def销毁
@user=user.find(参数[:id])
@用户破坏
回应待办事项|格式|
format.html{重定向到(用户url)}
format.xml{head:ok}
终止
终止
def是_用户
如果User.exists?(参数[:id])
@user=user.find(参数[:id]);
如果当前_user.admin_标志==true
flash[:notice]=“欢迎管理员”
终止
如果当前用户| |当前用户id!=@用户id
flash[:注意]=“您没有访问该页面的权限”
将_重定向到(:controller=>'home',:action=>'index')
终止
其他的
flash[:注意]=“您没有访问该页面的权限”
将_重定向到(:controller=>'home',:action=>'index')
终止
终止
终止
app/models/user.rb文件为:
class User < ActiveRecord::Base
acts_as_authentic
end
class用户
我可以确认admin_标志集用户已正确获取,因为文件app/views/layouts/application.html.erb包含:
<div id="admin">
<% if current_user %>
<% if current_user.admin_flag == true %> |
<%= link_to "Users", users_path %>
<% end %>
<% end %>
</div>
|
当我以管理员身份登录时,正确显示“用户”链接
现在的问题是,我无法显示所有用户,编辑其他用户等。。功能。作为管理员,我可以像所有其他普通用户一样显示和修改管理员用户,这意味着我也不能修改用户名
这里可能有什么问题?当您以正确的方式向用户添加布尔属性admin时,Rails应该添加问号方法admin?在用户模型中。这并不重要,只是为了方便。 在筛选前使用的每种方法上,您都希望避免不必要的操作:
class UsersController < ApplicationController
before_filter :admin_user, :only => :destroy
before_filter :correct_user, :only => [:edit, :update]
def destroy
end
...
private
def admin_user
redirect_to(root_path) unless current_user.admin?
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user) || current_user.admin?
end
end
class UsersController:destroy
在\u筛选器之前:更正\u用户,:only=>[:编辑,:更新]
def销毁
终止
...
私有的
def管理员用户
将_重定向到(根路径),除非当前_user.admin?
终止
def正确的用户
@user=user.find(参数[:id])
将_重定向到(根路径),除非当前_用户?(@user)| |当前_user.admin?
终止
终止
在视图中更方便地使用当前_user.admin
<div id="admin">
<% if current_user.admin? %>
<%= link_to "Users", users_path %>
<% end %>
</div>
谢谢您的回答。我应该提到我是rails的新手。那么is_用户函数呢?我应该完全放弃吗?而且我也不理解“根路径”。这只是首字母缩略词,还是法律参数。也就是说,我应该把它改成home#index或其他什么吗?再次检查我的消息,因为它出了问题。我认为is_用户功能设计得不好,因此您可以使用例如my note重新设计控制器。对于控制台中的根路径检查命令“rake routes”,您应该看到这个routeOk,现在我理解了根路径。非常感谢。但是现在的用户呢?给出错误:“#”的未定义方法“当前用户”。“is_user”函数在没有定义或声明有关当前_用户的任何内容的情况下没有给出此错误。如果你不介意的话,请简要解释一下这个错误的来源。我的意思是,我应该在哪里添加函数?我想我误解了MVC关系的某些部分,因为我试图从定制代码中学习,它很快在我的脑海中变得混乱。这是因为你没有为“当前用户”定义的方法。如果您有“当前用户”方法,我想您已经定义了这个方法。因为你是rails的新手,我建议你读一本免费的书,在那里你还可以读一些关于MVCSince的东西,我不能创建admin?方法,我更改了当前的\u user.admin?对于current_user.admin_flag==true,它解决了无方法问题,我对此并不感到羞耻:)我将完成guides.rubyonrails.org以及您建议的教程。但如果你有耐心的话,最后一个问题。我修改了用户的编辑视图,以便仅显示用户名,而不能更改用户名。要使管理员也能更改userna