Reputation: 1769
I am trying to toggle to administrator any user from my users list, but whenever I try to grant or to revoke the admin status, I am redirected to the "Something went wrong..." page.
I am following the Hartl's tutorial.
I'm just learning Rails. I try this as an exercise, I tried to make my admin_toggle method as the destroy method which works well. Unfortunately they don't work the same and I can't figure out why.
Here the code :
# 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
And the view :
# _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 %>
Weird thing, it redirects to /users/ID and gives me the page "Something went wrong..." with no trace as I can get any other time. When I reload /users/ID, it works, but still not modified.
Here are my routes if it can helps :
$ 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
Trace printed in server console : Loading the Users list. (/users)
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
Trace printed in server console : Clicking the Grant button for User 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)
Trace printed in server console : After reloading the webpage, clicking Revoke for User 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)
I don't understand why it is rooting to /users/5 while the :destroy
method roots to /users...
Upvotes: 0
Views: 146
Reputation: 336
admin = false is assignment, not condition checking. The following code should work
def admin_toggle
user = User.find(params[:id])
user.toggle!(:admin)
flash[:success] = user.admin ? "User granted" : "User revoked"
redirect_to users_url
end
after toggle, the user object gets updated, and admin attribute reflects the latest status
Make sure you added route for admin_toggle
resources :users do
put :admin_toggle, on: :member
end
and check the link_to tag to
<%= link_to "ADMIN : revoke", admin_toggle_user_path(user), method: :put, data: { confirm: "Sure ?" } %>
Upvotes: 2