我正在尝试从我的用户列表切换到管理员任何用户,但每当我尝试授予或撤消管理员状态时,我都会被重定向到"出现问题..."页。
我正在关注Hartl的教程。
我刚刚学习Rails。我尝试将其作为练习,我尝试将我的admin_toggle方法作为可以正常工作的destroy方法。不幸的是,他们不能这样做,我无法弄清楚原因。
这里是代码:
# users_controller.rb
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: [:destroy, :index, :admin_toggle]
...
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted"
redirect_to users_url
end
def admin_toggle
admin = User.find(params[:id]).admin
User.find(params[:id]).toggle!(:admin)
if admin == false
flash[:success] = "User granted"
redirect_to users_url
else
flash[:success] = "User revoked"
redirect_to users_url
end
end
观点:
# _user.html.erb (partial)
<% if current_user.admin? && !current_user?(user) %>
<% if user.admin? %>
| <%= link_to "ADMIN : revoke", user, method: :admin_toggle, data: { confirm: "Sure ?" } %>
<% else %>
| <%= link_to "NON ADMIN : grant", user, method: :admin_toggle, data: { confirm: "Sure ?" } %>
<% end %>
| <%= link_to "delete", user, method: :delete, data: { confirm: "Sure ?" } %>
<% end %>
奇怪的是,它重定向到/ users / ID并给我页面&#34;出了问题......&#34;没有任何痕迹,因为我可以得到任何其他时间。当我重新加载/ users / ID时,它可以工作,但仍未修改。
以下是我的路线,如果有帮助的话:
$ rake routes
Prefix Verb URI Pattern Controller#Action
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
signup GET /signup(.:format) users#new
create_operator GET /create_operator(.:format) operators#new
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
在服务器控制台中打印的跟踪:加载用户列表。 (/用户)
Started GET "/users" for ::1 at 2015-07-08 13:56:04 +0200
Processing by UsersController#index as HTML
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
(0.3ms) SELECT COUNT(*) FROM "users"
User Load (0.7ms) SELECT "users".* FROM "users" LIMIT 30 OFFSET 0
Rendered users/_user.html.erb (9.9ms)
Rendered users/index.html.erb within layouts/application (26.4ms)
Rendered layouts/_navbar.html.erb (1.6ms)
Rendered layouts/_footer.html.erb (0.4ms)
Completed 200 OK in 181ms (Views: 178.1ms | ActiveRecord: 1.1ms)
Started GET "/assets/bootstrap.min.self-6655d1384d1509415d234003e6f81cf5de3d688790aaadc1091aa5bb5795b1f1.css?body=1" for ::1 at 2015-07-08 13:56:05 +0200
Started GET "/assets/custom.self-5dd633f307143f76efffe6b3679cac893d31afe43812a2cb92157504e82a4c6f.css?body=1" for ::1 at 2015-07-08 13:56:05 +0200
Started GET "/assets/bootstrap_and_overrides.self-17173183fc31fb911be417d4e28b79f99d056440baa2cf49ce4f6bebb37313a5.css?body=1" for ::1 at 2015-07-08 13:56:05 +0200
Started GET "/assets/operators.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" for ::1 at 2015-07-08 13:56:05 +0200
Started GET "/assets/sessions.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" for ::1 at 2015-07-08 13:56:05 +0200
Started GET "/assets/users.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" for ::1 at 2015-07-08 13:56:05 +0200
Started GET "/assets/application.self-6ebd6dc00e43682c61d8f6c189788df291bca642abcb4b29fb5c478c41c371b6.css?body=1" for ::1 at 2015-07-08 13:56:05 +0200
Started GET "/assets/bootstrap.min.self-075878ec6a19d24f3b46052eb9e0e5bb5c2a098ac50d05d8e3a21309d129273a.js?body=1" for ::1 at 2015-07-08 13:56:05 +0200
Started GET "/assets/jquery.self-d03a5518f45df77341bdbe6201ba3bfa547ebba8ed64f0ea56bfa5f96ea7c074.js?body=1" for ::1 at 2015-07-08 13:56:05 +0200
Started GET "/assets/turbolinks.self-680e388bf7f759d85e2dbe0044d33f55e12b26da721317111c01ec92e53df55e.js?body=1" for ::1 at 2015-07-08 13:56:05 +0200
Started GET "/assets/operators.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for ::1 at 2015-07-08 13:56:05 +0200
Started GET "/assets/jquery_ujs.self-8e98a7a072a6cee1372d19fff9ff3e6aa1e39a37d89d6f06861637d061113ee7.js?body=1" for ::1 at 2015-07-08 13:56:05 +0200
Started GET "/assets/sessions.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for ::1 at 2015-07-08 13:56:05 +0200
Started GET "/assets/users.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for ::1 at 2015-07-08 13:56:05 +0200
Started GET "/assets/application.self-f8806224e027f3e3f0138ea9ce99319e298dfdb323304d1f1be6eae8e8c74724.js?body=1" for ::1 at 2015-07-08 13:56:05 +0200
在服务器控制台中打印的跟踪:单击“用户4”的“授予”按钮
Started POST "/users/4" for ::1 at 2015-07-08 13:58:18 +0200
ActionController::RoutingError (No route matches [POST] "/users/4"):
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:22:in `middleware_call'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.2) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.2) lib/rails/engine.rb:518:in `call'
railties (4.2.2) lib/rails/application.rb:164:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
puma (2.11.1) lib/puma/server.rb:507:in `handle_request'
puma (2.11.1) lib/puma/server.rb:375:in `process_client'
puma (2.11.1) lib/puma/server.rb:262:in `block in run'
puma (2.11.1) lib/puma/thread_pool.rb:104:in `call'
puma (2.11.1) lib/puma/thread_pool.rb:104:in `block in spawn_thread'
Rendered /Users/tomsihap/.rvm/gems/ruby-2.2.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_trace.html.erb (2.8ms)
Rendered /Users/tomsihap/.rvm/gems/ruby-2.2.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/routing_error.html.erb within rescues/layout (69.3ms)
在服务器控制台中打印的跟踪:重新加载网页后,点击撤消用户5
Started POST "/users/5" for ::1 at 2015-07-08 14:00:17 +0200
ActionController::RoutingError (No route matches [POST] "/users/5"):
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:22:in `middleware_call'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.2) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.2) lib/rails/engine.rb:518:in `call'
railties (4.2.2) lib/rails/application.rb:164:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
puma (2.11.1) lib/puma/server.rb:507:in `handle_request'
puma (2.11.1) lib/puma/server.rb:375:in `process_client'
puma (2.11.1) lib/puma/server.rb:262:in `block in run'
puma (2.11.1) lib/puma/thread_pool.rb:104:in `call'
puma (2.11.1) lib/puma/thread_pool.rb:104:in `block in spawn_thread'
Rendered /Users/tomsihap/.rvm/gems/ruby-2.2.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_trace.html.erb (2.2ms)
Rendered /Users/tomsihap/.rvm/gems/ruby-2.2.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/routing_error.html.erb within rescues/layout (66.2ms)
当:destroy
方法根源于/ users ...
答案 0 :(得分:2)
admin = false是赋值,而不是条件检查。 以下代码应该可以使用
def admin_toggle
user = User.find(params[:id])
user.toggle!(:admin)
flash[:success] = user.admin ? "User granted" : "User revoked"
redirect_to users_url
end
切换后,用户对象会更新,admin属性会反映最新状态
确保为admin_toggle
添加了路线resources :users do
put :admin_toggle, on: :member
end
并检查link_to标记为
<%= link_to "ADMIN : revoke", admin_toggle_user_path(user), method: :put, data: { confirm: "Sure ?" } %>