2016-03-19 93 views
0

我如何管理和编辑其他用户配置文件作为管理员,因为我有一个模型和控制器(用户)?由管理员在轨道上的红宝石管理用户

我尝试添加一个新的动作称为updateusers

def updateusers 
    @other_user=User.find(params[:id]) 
    if @other_user.update_attributes(otherusers_params) 
      redirect_to '/' 
    else 
      redirect_to '/manage' 
    end 
end 

这里的问题:它更新与other_user的数据我的管理员用户

堆栈跟踪
开始GET“/管理” for :: 1 at 2016-03-19 21:06:08 +0300 UsersController处理#管理为HTML用户负载(1.0ms)SELECT“users”。* FROM“users”在布局中呈现用户/ manage.html.erb/application(5.0ms)User Load(0.0ms)SELECT“users”。* FROM“users”WHERE“users”。“id”= ? LIMIT 1 [[“id”,1]]在53ms内完成200 OK(查看:51.0ms | ActiveRecord:1.0ms)

'GET GET/users/10 for :: 1在2016-03-19 21:06:10 +0300 UsersController#显示为HTML参数:{“id”=>“10”}用户负载(0.0ms)SELECT“users”。* FROM“users”WHERE“users”。“id” =? LIMIT 1 [[“id”,10]]在布局/应用程序中呈现用户/ show.html.erb(0.0ms)用户载入(0.0ms)SELECT“users”。* FROM“users”WHERE“users”。“id “=? LIMIT 1 [[“id”,1]]在37ms内完成200 OK(查看:36.0ms | ActiveRecord:0.0ms)

开始GET“/ editusers/10”为:: 1在2016-03-19 21 :06:11 +0300 UsersController#editusers处理HTML参数:{“id”=>“10”}用户载入(0.0ms)SELECT“users”。* FROM“users”WHERE“users”。“id”= ? LIMIT 1 [[“id”,10]]在layouts/application(4.0ms)内呈现users/editusers.html.erb用户载入(1.0ms)SELECT“users”。* FROM“users”WHERE“users”。“id “=? LIMIT 1 [[“id”,1]]在41ms内完成200 OK(查看:39.0ms | ActiveRecord:1.0ms)

2016-03-19 21开始PATCH“/ users/10”:: 1 :06:15 +0300 UsersController处理#更新为HTML参数:{“utf8”=>“✓”,“authenticity_token”=>“6M1TGLQUEhiezCCg9/rT5IofdroMiQ0sm + bYcihgGDxTjDdFGU2Riou2p cRk5ncjCtFDGwfBj17Uq7gc0u329w ==”,“user”=> {“first_name “=”g“,”last_name“=>”g“,”email“=>”g @ gg“,”role“=>”editor“,”image“=>”pic.png“,” =>“”},“other”=>“update”,“id”=>“10”}用户载入(0.0ms)SELECT“users”。* FROM“users”WHERE“users”。“id”=? LIMIT 1 [[“id”,1]] 不允许的参数:角色,管理员

(0.0ms)begin transaction SQL(1.0ms)UPDATE“users”SET“first_name”=?,“last_name”=?, “email”=?,“updated_at”=? WHERE“users”。“id”=? [[“first_name”,“g”],[“last_name”,“g”],[“email”,“g @ gg”],[“updated_at”,“2016-03-19 18:06:15.488284” ],[ “ID”,1]](47.0ms)提交事务重定向到本地主机:8080 /简档已完成302在54ms实测值(ActiveRecord的:48.0ms)

+0

的更新操作你传递的参数'params [:id]'是给其他用户的? – psantos

+0

我建议使用activeadmin gem,而不是编写自己的管理系统。 http://activeadmin.info/ –

+0

是 在显示和编辑视图给我的数据其他用户 –

回答

0

10天后,,是的,我做到了 - 该解决方案是在的名义提交,我又点了两位提交带有不同势名字<%= f.submit "update", name:"other" %> 然后我用这样的

def update 
    if params[:current] 
     @user = current_user 
     if @user.update_attributes(user_params) 
      redirect_to '/profile' 
     else 
      redirect_to '/edit' 
     end 

    elsif params[:other] 
     @other_user=User.find(params[:id]) 
     if @other_user.update_attributes(otherusers_params) 
      redirect_to '/' 
      else 
      redirect_to '/manage' 
      end 
    end 

end 
1

如果它更新错误的用户,这意味着params[:id]是正在更新的用户的ID。你是否在params中传递了你想更新的用户的ID?尝试在控制器操作的顶部调用puts params.inspect以查看正在传递的数据。您需要查看带有ID的@other_user,并且需要确保@ other_user的ID与其他表单数据一起传递。

+0

我认为它不是读取新的更新动作(updateusers),它仍然读取原来的更新动作??!我有两个更新操作,一个用于current_user,一个用于管理员更新其他用户 –

+0

你有'route.rb'中的updateusers路由吗? – toddmetheny

+0

patch'/ users/update'=>'users#update' - 原来的 ---- patch'users /:id/updateusers'=>'users#updateusers' put'users /:id/updateusers '=>'users#updateusers' –