ruby on rails - How to use link_to with a method and how to route it? -


i trying toggle administrator user users list, whenever try grant or revoke admin status, redirected "something went wrong..." page.

i following hartl's tutorial.

i'm learning rails. try exercise, tried make admin_toggle method destroy method works well. unfortunately don't work same , can't figure out why.

here 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 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, redirects /users/id , gives me page "something went wrong..." no trace can other time. when reload /users/id, works, still not modified.

here routes if can helps :

    $ rake routes          prefix verb   uri pattern                   controller#action           login    /login(.:format)              sessions#new                 post   /login(.:format)              sessions#create          logout delete /logout(.:format)             sessions#destroy          signup    /signup(.:format)             users#new create_operator    /create_operator(.:format)    operators#new           users    /users(.:format)              users#index                 post   /users(.:format)              users#create        new_user    /users/new(.:format)          users#new       edit_user    /users/:id/edit(.:format)     users#edit            user    /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 users list. (/users)

started "/users" ::1 @ 2015-07-08 13:56:04 +0200 processing userscontroller#index html   user load (0.1ms)  select  "users".* "users" "users"."id" = ? limit 1  [["id", 1]]    (0.3ms)  select count(*) "users"   user load (0.7ms)  select  "users".* "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 "/assets/bootstrap.min.self-6655d1384d1509415d234003e6f81cf5de3d688790aaadc1091aa5bb5795b1f1.css?body=1" ::1 @ 2015-07-08 13:56:05 +0200 started "/assets/custom.self-5dd633f307143f76efffe6b3679cac893d31afe43812a2cb92157504e82a4c6f.css?body=1" ::1 @ 2015-07-08 13:56:05 +0200 started "/assets/bootstrap_and_overrides.self-17173183fc31fb911be417d4e28b79f99d056440baa2cf49ce4f6bebb37313a5.css?body=1" ::1 @ 2015-07-08 13:56:05 +0200 started "/assets/operators.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" ::1 @ 2015-07-08 13:56:05 +0200 started "/assets/sessions.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" ::1 @ 2015-07-08 13:56:05 +0200 started "/assets/users.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" ::1 @ 2015-07-08 13:56:05 +0200 started "/assets/application.self-6ebd6dc00e43682c61d8f6c189788df291bca642abcb4b29fb5c478c41c371b6.css?body=1" ::1 @ 2015-07-08 13:56:05 +0200 started "/assets/bootstrap.min.self-075878ec6a19d24f3b46052eb9e0e5bb5c2a098ac50d05d8e3a21309d129273a.js?body=1" ::1 @ 2015-07-08 13:56:05 +0200 started "/assets/jquery.self-d03a5518f45df77341bdbe6201ba3bfa547ebba8ed64f0ea56bfa5f96ea7c074.js?body=1" ::1 @ 2015-07-08 13:56:05 +0200 started "/assets/turbolinks.self-680e388bf7f759d85e2dbe0044d33f55e12b26da721317111c01ec92e53df55e.js?body=1" ::1 @ 2015-07-08 13:56:05 +0200 started "/assets/operators.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" ::1 @ 2015-07-08 13:56:05 +0200 started "/assets/jquery_ujs.self-8e98a7a072a6cee1372d19fff9ff3e6aa1e39a37d89d6f06861637d061113ee7.js?body=1" ::1 @ 2015-07-08 13:56:05 +0200 started "/assets/sessions.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" ::1 @ 2015-07-08 13:56:05 +0200 started "/assets/users.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" ::1 @ 2015-07-08 13:56:05 +0200 started "/assets/application.self-f8806224e027f3e3f0138ea9ce99319e298dfdb323304d1f1be6eae8e8c74724.js?body=1" ::1 @ 2015-07-08 13:56:05 +0200 

trace printed in server console : clicking grant button user 4

started post "/users/4" ::1 @ 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 webpage, clicking revoke user 5

started post "/users/5" ::1 @ 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 rooting /users/5 while :destroy method roots /users...

admin = false assignment, not condition checking. 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, user object gets updated, , admin attribute reflects latest status

make sure added route admin_toggle

resources :users   put :admin_toggle, on: :member end 

and check link_to tag to

<%= link_to "admin : revoke", admin_toggle_user_path(user), method: :put, data: { confirm: "sure ?" } %> 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -