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的 數據我的管理員用戶

堆棧跟蹤

Started GET "/manage" for ::1 at 2016-03-19 21:06:08 +0300 Processing by 
    UsersController#manage as HTML User Load (1.0ms) SELECT "users".* FROM 
"users" Rendered users/manage.html.erb within layouts/application (5.0ms) User 
Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 
[["id", 1]] Completed 200 OK in 53ms (Views: 51.0ms | ActiveRecord: 1.0ms) 


    'Started GET "https://stackoverflow.com/users/10" for ::1 at 2016-03-19 21:06:10 +0300 Processing by 
UsersController#show as HTML Parameters: {"id"=>"10"} User Load (0.0ms) SELECT 
"users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]] Rendered 
users/show.html.erb within layouts/application (0.0ms) User Load (0.0ms) SELECT 
"users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] Completed 200 
OK in 37ms (Views: 36.0ms | ActiveRecord: 0.0ms) 


    Started GET "/editusers/10" for ::1 at 2016-03-19 21:06:11 +0300 Processing 
by UsersController#editusers as HTML Parameters: {"id"=>"10"} User Load (0.0ms) 
SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]] 
Rendered users/editusers.html.erb within layouts/application (4.0ms) User Load 
(1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] 
Completed 200 OK in 41ms (Views: 39.0ms | ActiveRecord: 1.0ms) 


    Started PATCH "https://stackoverflow.com/users/10" for ::1 at 2016-03-19 21:06:15 +0300 Processing by 
UsersController#update as HTML Parameters: {"utf8"=>"✓", 
"authenticity_token"=>"6M1TGLQUEhiezCCg9/rT5IofdroMiQ0sm+bYcihgGDxTjDdFGU2Riou2p‌​ 
cRk5ncjCtFDGwfBj17Uq7gc0u329w==", "user"=>{"first_name"=>"g", "last_name"=>"g", 
"email"=>"[email protected]", "role"=>"editor", "image"=>"pic.png", "admins"=>""}, 
"other"=>"update", "id"=>"10"} User Load (0.0ms) SELECT "users".* FROM "users" 
WHERE "users"."id" = ? LIMIT 1 [["id", 1]] Unpermitted parameters: role, admins 


    (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", "[email protected]"], ["updated_at", 
"2016-03-19 18:06:15.488284"], ["id", 1]] (47.0ms) commit transaction Redirected 
to localhost:8080/profile Completed 302 Found in 54ms (ActiveRecord: 48.0ms) 

回答

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 
0

「editusers」中表單的用戶標識設置爲您的管理員(或登錄用戶)。沒有看到代碼很難說,但我認爲你已經建立了editusers形式不正確。也許使用隱藏字段來保存要更新的用戶的ID。

儘量避免在您的視圖中使用一個form_for @user do |f|沒有爲ID的隱藏字段建立@user對象在「editusers」行動@user = User.find(10) 然後。

+0

非常感謝你 –

+0

我希望它有幫助! – meshpi