简体   繁体   English

Ruby on Rails 用户控制器的自定义控制器操作

[英]Ruby on Rails Custom Controller Action for Users Controller

Working on versions: Ruby: 3.0.2 Rails 6.1.4处理版本: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".我正在尝试在“用户”的视图模板中放置一个按钮,该按钮会将 :mod 属性设置为 false,而该按钮只是“降级用户”。

I had it working SOMEHOW using a helper method, demote_user, and some variation of我使用辅助方法、demote_user 和一些变体让它以某种方式工作

  <% 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.现在,每次我加载用户页面时,它只是执行 demote_user(@user) 甚至不需要单击按钮,因此刷新用户页面正在降级它们。

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.我现在正试图通过在 UserController 中创建一个降级方法来正确执行此操作,但我不知道如何制作路由或使其正常工作。 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"我一直在尝试许多不同的路线,并查看格式,但没有运气,通常以: 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.我希望能够避免使用 Javascript onClick 功能并使用控制器正确执行此操作,即使我记得如何使其工作,但我认为我的路线、视图页面或控制器不正确。 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.我什至不确定 POST 是否是路线中的正确调用。

I would just setup an additional RESTful action for the resource :我只想为资源设置一个额外的 RESTful 操作

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.您可以使用link_to代替button_to ,但您需要使用正确的method: :patchdata: { turbo_method: :patch }选项,具体取决于您的 Rails 版本,以使 javascript 驱动程序执行其诡计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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