简体   繁体   中英

Ruby on Rails Custom Controller Action for Users Controller

Working on versions: Ruby: 3.0.2 Rails 6.1.4

I'm trying to put a button in my View template for "Users" that will set the :mod attribute to false, with the button just being "Demote User".

I had it working SOMEHOW using a helper method, demote_user, and some variation of

  <% if logged_in? && current_user.admin? %>
      <%= link_to "Demote User", user_path(@user), onclick: demote_user(@user), class: "btn btn-info" %>
  <% end %>

I messed up however it was working when trying to move this method into a controller for best practice. I'm not sure what format worked out, but I've tried many such as

 :onclick => demote_user(@user)
 onclick: 'demote_user(@user)'
 onclick=demote_user(@user)
 onclick='demote_user(@user)

and etc. Somehow, it did eventually work but I busted it. Now, every time I load the User's page, it's just executing demote_user(@user) without even needing to click the button, so refreshing a User's page is demoting them.

I am now trying to do this properly by creating a demote method in the UserController, but I have NO IDEA how to make the route, or make it work properly. Up until now, all my routes have been working using resources, and the basic new, create, destroy, etc.

I've been trying many different routes, and view formatting, with no luck and usually ending up with just: Routing Error No route matches [GET] "/demote.3"

I'd like to be able to avoid using the Javascript onClick functionality and do it properly with the controller even if I could remember how to make it work, but I think my route, or view page, or controller is incorrect. Here are the contents of each file:

Controller

def demote
  byebug
  @user = User.find(params[:id])
  @user.mod = false
  redirect_to user_path(@user)
end

View

div class="container text-center mt-4">
  <% if logged_in? && current_user.admin? %>
      <%= link_to "Demote User", demote_path(@user), class: "btn btn-info" %>
  <% end %>
</div>

Routes

Rails.application.routes.draw do

  root 'pages#home'
  get 'about', to: 'pages#about'
  resources :articles
  get 'signup', to: 'users#new'
  resources :users, except: [:new]
  get 'login', to: 'sessions#new'
  post 'login', to: 'sessions#create'
  delete 'logout', to: 'sessions#destroy'
  resources :categories
  post 'demote', to: 'users#demote'
end

Up until now, I've been following a course, and I've nailed everything in it and have added some of my own functionality, but I'm still not quite sure how to make sense of routes, or what paths it creates.

I'd like to make other similar custom controller functions beyond show, index, create, delete, update which are all I really understand how to build. I'm not even sure if POST is the correct call in the route.

I would just setup an additional RESTful action for the resource :

resources :users do
  patch :demote
end
<% if logged_in? && current_user.admin? %>
  <%= button_to "Demote User", demote_user_path(@user), method: :patch %>
<% end %>

You can use link_to instead of button_to but you need to use the correct method: :patch or data: { turbo_method: :patch } option depending on your version of Rails to get the javascript driver to do its trickery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM