【发布时间】:2018-05-13 10:47:41
【问题描述】:
我希望管理员可以将带有 link_to 的 :coach 的布尔状态更改为“真”,当它为“假”时,变为“假”,当它为“真”时。喜欢删除用户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 %>
控制器/用户控制器:
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
Route.rb:
resources :users
get 'signup' => 'users#new'
post '/signup' => 'users#create'
resources :user do
post :coach
end
实际错误:
没有路由匹配 [POST] "/users/1"
不知道如何设置路线。 我尝试了很多东西,但没有任何效果,我是开发和 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 %>
但这部分可供所有用户访问,而切换教练仅供管理员访问
【问题讨论】:
-
你知道是哪一行触发了错误吗?另外,
coach: => :false这个语法在我看来是错误的,应该是coach: :false或:coach => :false -
尝试用
<%= link_to "Passer coach", edit_user_path(user), coach: :true, data: { confirm: "Êtes vous sûr ?" } %>替换这个<%= link_to "Passer coach", user, coach: => :true, method: :edit, data: { confirm: "Êtes vous sûr ?" } %> -
我试过你的解决方案,没有错误信息。我被重定向到 root 并且没有考虑到更改
-
你有
users/edit视图吗? -
查看您的终端/控制台窗口并检查正在调用哪个控制器操作。点击链接后会有一行
Processing by UsersController#update as HTML。此外,您的coach参数的格式不正确,无法与您的user_params方法匹配 - 您可能想要类似'user[coach]': :true
标签: ruby-on-rails ruby ruby-on-rails-4