管理员如何使用link_to更改布尔用户字段?



我希望管理员可以将 :coach 的布尔状态更改为 link_to 当它"假"时为"真",当它为"真"时更改为"假"。就像删除用户link_to一样。

视图/用户/索引 :

<%= will_paginate %>
<ul class="users">
<%= render @users %>
</ul>
<%= will_paginate %>

视图/用户/_user :

<li>
<%= link_to image_tag(user.photo.url(:thumb)), user %>
<%= link_to user.fname, user %>
<%= link_to user.lname, user %>
<%= user.email %>
<%= user.coach %>
<% if current_user.admin? && !current_user?(user) %>
<% if user.coach == false %>
| <%= link_to "Passer coach", user, coach: => :true, method: :edit, 
data: { confirm: "Êtes vous sûr ?" } %>
<% else %>
| <%= link_to "Retirer des coachs", user, coach: => :false, method: :edit, 
data: { confirm: "Êtes vous sûr ?" } %>
<% end %>
<% end %>

<% if current_user.admin? && !current_user?(user) %>
| <%= link_to "Supprimer", user, method: :delete,
data: { confirm: "Êtes vous sûr ?" } %>
<% end %> 

控制器/users_controller :

def edit
@user = User.find(params[:id])
end
def update
if @user.update_attributes(user_params)
flash[:success] = "Profil mis à jour"
redirect_to @user
else
render 'edit'
end
end
def index
@users = User.where(activated: true).paginate(page: params[:page])
end
private
def user_params
params.require(:user).permit(:fname, :lname, :email, :password,
:password_confirmation, :photo,    :birthdate, :mother_tongue, :coach, spoken_language_ids:[])
end

路线.rb :

resources :users
get 'signup' => 'users#new'
post '/signup' => 'users#create'
resources :user do
post :coach
end

实际错误 :

没有与 [POST] "/users/1" 匹配的路由

不知道我该怎么放路线。 我尝试了很多东西,但没有任何效果,我是 dev 和 Ruby 的新手,真的需要你的帮助,非常感谢。

这是我的用户/编辑视图:

<% provide(:title, "Paramètres") %>
<h1>Mettre à jour votre profil</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for (@user), :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :photo %>
<%= f.file_field :photo, class: 'form-control' %>
<%= f.label :Prénom %>
<%= f.text_field :fname, class: 'form-control' %>
<%= f.label :Nom %>
<%= f.text_field :lname, class: 'form-control' %>
<%= f.label :Date_de_naissance %>
<%= f.date_select :birthdate, class: 'form-control' %>
<%= f.label :Langue_maternelle %>
<%= f.text_field :mother_tongue, class: 'form-control' %>
<%= f.label :Autres_langues_parlées %>
<%= f.collection_check_boxes :spoken_language_ids, SpokenLanguage.all, :id, :name do |b| %>
<div class="collection-check-box">
<%= b.check_box %>
<%= b.label %>
<% end %>
<%= f.label :Email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :Mot_de_passe %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :Confirmation_mot_de_passe, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Enregistrer les changements", class: "btn btn-primary" %>
<% end %>

但是这部分可供所有用户访问,而切换教练仅供管理员访问

当您定义通往post :coach的路由时,您需要将其用作link_to
的路径 正是在这个路径中添加了所需的参数(用户和 coach 属性 true/false(
此路径必须指向控制器内的方法。

您的自定义路线:

resources :users do
post :coach
end
#=> output: user_coach POST /users/:user_id/coach(.:format)  users#coach

在索引视图中(在用户循环内(:

<% if current_user.admin? && !current_user?(user) %>
<% if user.coach %>
| <%= link_to "Retirer des coachs", user_coach_path(user, coach: false), method: :post %>
<% else %>
| <%= link_to "Passer coach", user_coach_path(user, coach: true), method: :post %>
<% end %>
<% end %>

注意:由于user.coach返回一个布尔值,因此您可以直接将其用作if/else语句的条件

然后定义控制器方法:

def coach
user = User.find(params[:user_id])
user.update(coach: params[:coach])
redirect_to users_path
end

最新更新