我按照http://www.logansbailey.com/的教程进行了修改,并对其进行了修改,以使未注册的人员能够使用用户名,电子邮件和密码进行注册。
我已经启用了登录用户修改他/她的电子邮件和密码,但不是用户名。如何将一个普通用户分配为超级用户或管理员
我想补充的是:
1),以使登录的用户才能够看到/到达他/她的用户名和电子邮件,
2),以实现与admin_flag集合中的用户(我在sql表中处理这个并创建用户),以便能够查看/修改所有用户记录。
我modifyed这样的应用程序/ 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
文件app /模型/ user.rb是:
而且我可以证实,admin_flag集用户得到正确,因为文件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>
correct当我以管理员身份登录时,会显示“用户”链接。
现在的问题是,我无法显示所有用户,编辑其他用户等功能。作为管理员,我可以像所有其他普通用户一样显示和修改管理员用户,这意味着我也无法修改用户名。
这里可能有什么问题?
谢谢您的回答。我应该提到我是一名新手。那么is_user函数呢?我应该完全放弃吗?而且我也不理解“root_path”。这只是一个缩写词,还是一个法律参数。所以说,我应该改变它到家庭#索引或其他什么? – barerd 2011-04-09 11:41:08
再次检查我的消息,因为它出错了。我认为is_user功能设计不好,所以你可以使用例如我的笔记重新设计你的控制器。对于root_path检查您的控制台命令'rake routes',在那里你应该看到这条路线 – megas 2011-04-09 11:51:27
好吧,现在我明白了root_path。谢谢。但是current_user?给出错误:“未定义的方法'current_user?'为#“。没有定义或声明关于current_user的任何东西,'is_user'函数没有给出这个错误。如果你不介意,你会简要解释一下这个错误的来源。我的意思是,我应该在哪里添加该功能?我想我误解了MVC关系的一部分,因为我试图从定制代码中学习,所以它很快就被搞乱了。 –
barerd
2011-04-09 12:01:32