【发布时间】:2014-10-08 13:38:57
【问题描述】:
我目前有一个名为“用户”的视图页面。在我的视图页面中,我正在显示一个表,其中包含从用户表中提取的属性,例如名字、姓氏、上次登录、管理员等......在管理字段中,它从用户表中显示 true 或 false .
我想要完成的是创建一个下拉列表,显示用户当前的 admin 值(true 或 false),然后能够从视图页面更改此属性。目前,我必须使用服务器上的控制台手动将用户的值更改为 true 或 false 以授予或撤销管理员权限。不用说,这很不方便。我对 Rails 还很陌生,到目前为止一直在使用脚手架。我不知道如何做到这一点,任何建议将不胜感激!
用户架构:
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "admin", default: false
t.string "first_name"
t.string "last_name"
end
管理控制器:
class AdminController < ApplicationController
before_action :authenticate_user!
def users
@users = User.all.order('last_name ASC')
end
def reports
end
end
用户查看页面:
<table class="table table-hover table-condensed">
<thead>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Email</th>
<th>Creation Date</th>
<th>Last Sign In</th>
<th>IP Address</th>
<th>Admin</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.last_name %></td>
<td><%= user.first_name %></td>
<td><%= user.email %></td>
<td><%= user.created_at.strftime("%-m/%-d/%y") %></td>
<td><%= user.current_sign_in_at.strftime("%-m/%-d/%y") %></td>
<td><%= user.current_sign_in_ip %></td>
<td><%= user.admin %></td>
</tr>
<% end %>
</tbody>
</table>
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-4.1