【问题标题】:How admin can change boolean users field with link_to?管理员如何使用 link_to 更改布尔用户字段?
【发布时间】: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: =&gt; :false 这个语法在我看来是错误的,应该是 coach: :false:coach =&gt; :false
  • 尝试用&lt;%= link_to "Passer coach", edit_user_path(user), coach: :true, data: { confirm: "Êtes vous sûr ?" } %&gt;替换这个&lt;%= link_to "Passer coach", user, coach: =&gt; :true, method: :edit, data: { confirm: "Êtes vous sûr ?" } %&gt;
  • 我试过你的解决方案,没有错误信息。我被重定向到 root 并且没有考虑到更改
  • 你有users/edit 视图吗?
  • 查看您的终端/控制台窗口并检查正在调用哪个控制器操作。点击链接后会有一行Processing by UsersController#update as HTML。此外,您的 coach 参数的格式不正确,无法与您的 user_params 方法匹配 - 您可能想要类似 'user[coach]': :true

标签: ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

当您定义到post :coach 的路线时,您需要将其用作link_to 的路径
您可以在此路径中添加所需的参数(用户和教练属性 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

【讨论】:

  • 成功了!数据库中的布尔变化!多谢。但它返回一个我以前从未见过的错误:ActionController::UnknownFormat in UsersController#coach UsersController#coach 缺少此请求格式和变体的模板。 request.formats: ["text/html"] request.variant: []
  • @BenjaminRichard 在 Coach 方法中尝试redirect_to users_path(在user.update 之后)
  • 我输入了def coach user = User.find(params[:user_id]) user.update(coach: params[:coach]) redirect_to users_url end,它工作了:)
猜你喜欢
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
  • 2011-02-02
  • 1970-01-01
相关资源
最近更新 更多