- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个编辑 View ,允许我更新特定用户的角色。但是,我已经包含了下面的代码,只是为了给您一个想法,当我取消选中页面上的框时,我的 Controller 中的 puts 语句正确地指出角色关联是错误的,并且可以在日志中进一步查看.
但是,稍后在日志中,您可以看到角色正在重新关联回“true”,我完全不知道为什么会这样!
如果未选中复选框,我希望从用户中删除这些角色。
用户/edit.html.erb:
<fieldset>
<%= form_for @user, :url => {action: "update"}, :html => { :class => 'user-role' } do |f| %>
<h1 class="h1-heading">User Roles</h1>
<p class="user-paragraph"> Check the boxes to grant different roles to <%= @user.first_name %> <%= @user.last_name %>:</p>
<%= f.label(:admin) do %>
<%= hidden_field_tag(:admin, 0) %>
<%= check_box_tag(:admin, 1, @user.has_role?(:admin)) %>
Administrator
<% end %>
<%= f.label(:member) do %>
<%= hidden_field_tag(:member, 0) %>
<%= check_box_tag(:member, 1, @user.has_role?(:member)) %>
Member
<% end %>
<%= f.submit class: 'btn btn-primary-dialog pull-right' %>
<% end %>
</fieldset>
def update
@user = User.find(params[:id])
@customer = current_user.customer
@logged_in_user = User.find_by_email(current_user.email)
if params[:admin] == "1"
@user.grant(:admin)
elsif params[:admin] == "0"
@user.remove_role(:admin)
end
if params[:member] == "1"
@user.grant(:member)
elsif params[:member] == "0"
@user.remove_role(:member)
end
puts "NEW ROLES"
puts @user.has_role? :member
puts @user.has_role? :admin
if @user.update_attributes(params[:user])
puts "UPDATING USER"
puts @user.has_role? :member
puts @user.has_role? :admin
redirect_to '/users/show', :flash => { :alert => 'User was successfully updated.' }
end
end
end
Started PATCH "/users/51" for 127.0.0.1 at 2015-06-26 13:48:58 +1000
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"+qNti57HxMa2B/a+c6nS71Qp0p7hf+kTE4b5eiBI4No=", "admin"=>"0", "member"=>"0", "commit"=>"Update User", "id"=>"51"}
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
Customer Load (0.3ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`email` = 'ryan@ryandrake.com' LIMIT 1
Company Load (0.2ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`domain` = 'ryandrake.com' LIMIT 1
CustomerAccess Load (0.3ms) SELECT `customer_accesses`.* FROM `customer_accesses` WHERE `customer_accesses`.`customer_id` = 1 LIMIT 1
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 51 LIMIT 1
CACHE (0.0ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`email` = 'ryan@ryandrake.com' LIMIT 1
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'ryan@ryandrake.com' LIMIT 1
Role Load (0.3ms) SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 51 AND `roles`.`name` = 'admin'
(0.1ms) BEGIN
(0.2ms) DELETE FROM `users_roles` WHERE `users_roles`.`user_id` = 51 AND `users_roles`.`role_id` IN (9)
(0.9ms) COMMIT
(0.3ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM `users` INNER JOIN `users_roles` ON `users`.`id` = `users_roles`.`user_id` WHERE `users_roles`.`role_id` = 9 LIMIT 1) subquery_for_count
Role Load (0.3ms) SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 51 AND `roles`.`name` = 'member'
(0.1ms) BEGIN
(0.2ms) DELETE FROM `users_roles` WHERE `users_roles`.`user_id` = 51 AND `users_roles`.`role_id` IN (3)
(0.4ms) COMMIT
(0.5ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM `users` INNER JOIN `users_roles` ON `users`.`id` = `users_roles`.`user_id` WHERE `users_roles`.`role_id` = 3 LIMIT 1) subquery_for_count
NEW ROLES
Role Load (0.7ms) SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 51 AND (((roles.name = 'member') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
false
Role Load (0.6ms) SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 51 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
false
(0.3ms) BEGIN
Customer Load (0.3ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`email` = 'ryan.drake2@otherlevels.com' LIMIT 1
ExtraCustomerAccount Load (0.6ms) SELECT `extra_customer_accounts`.* FROM `extra_customer_accounts` WHERE `extra_customer_accounts`.`email` = 'ryan.drake2@otherlevels.com' LIMIT 1
Customer Load (0.3ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 1 LIMIT 1
Company Load (0.2ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`domain` = 'otherlevels.com' LIMIT 1
Company Load (0.2ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`name` = 'None' ORDER BY `companies`.`id` ASC LIMIT 1
Role Load (0.3ms) SELECT `roles`.* FROM `roles` WHERE `roles`.`name` = 'member' AND `roles`.`resource_type` IS NULL AND `roles`.`resource_id` IS NULL ORDER BY `roles`.`id` ASC LIMIT 1
Role Exists (0.2ms) SELECT 1 AS one FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 51 AND `roles`.`id` = 3 LIMIT 1
(0.2ms) SELECT `roles`.id FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 51
Role Load (0.2ms) SELECT `roles`.* FROM `roles` WHERE `roles`.`id` = 3 LIMIT 1
Role Load (0.3ms) SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 51
(0.2ms) INSERT INTO `users_roles` (`user_id`, `role_id`) VALUES (51, 3)
Role Load (0.6ms) SELECT `roles`.* FROM `roles` WHERE `roles`.`name` = 'admin' AND `roles`.`resource_type` IS NULL AND `roles`.`resource_id` IS NULL ORDER BY `roles`.`id` ASC LIMIT 1
Role Load (0.3ms) SELECT `roles`.* FROM `roles` WHERE `roles`.`id` IN (3, 9)
(0.2ms) INSERT INTO `users_roles` (`user_id`, `role_id`) VALUES (51, 9)
(0.3ms) COMMIT
UPDATING USER
Role Load (0.4ms) SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 51 AND (((roles.name = 'member') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
true
Role Load (0.3ms) SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 51 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
true
Role Load (0.3ms) SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 1 AND (((roles.name = 'otherlevels_admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
Redirected to http://localhost:3000/users/show
Completed 302 Found in 80ms (ActiveRecord: 15.1ms)
最佳答案
你的主要问题在这里:@user.update_attributes(params[:user_id])
编辑 - 我原来的解决方案根本不起作用。
与我原来的解决方案不同,这不需要简单的形式。
首先让我们设置我们的表单:
<%= form_for(@user, url: {action: "update"}, html: { class: 'user-role' }) do |f| %>
<h1 class="h1-heading">User Roles</h1>
<p class="user-paragraph"> Check the boxes to grant different roles to <%= @user.first_name %> <%= @user.last_name %>:</p>
<%= f.fields_for(:roles) do |r| %>
<%= r.hidden_field :name unless r.object.persisted? %>
<%= r.label :_keep do %>
<%= r.check_box :_keep, checked: r.object.persisted? %>
<%= r.object.name %>
<% end %>
<% end %>
<% end %>
name
(新角色)_keep
虚拟属性 - 我们是否保存角色 id
(如果角色存在,由 rails 自动插入)accepts_nested_attributes_for :roles
class User < ActiveRecord::Base
rolify
accepts_nested_attributes_for :roles,
allow_destroy: true,
reject_if: ->(hash){ hash["_keep"] != "1" }
end
reject_if: ->(hash){ hash["_keep"] != "1" }
这意味着如果未选中复选框,我们不会创建角色,如果我们通过
_delete=true
,allow_destroy 将删除角色。 .
_keep
角色的虚拟属性:
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource, :polymorphic => true
validates :resource_type,
:inclusion => { :in => Rolify.resource_types },
:allow_nil => true
scopify
attr_accessor :_keep
AVAILABLE_ROLES = %w{ administrator member }
end
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# ...
# GET /users/1/edit
def edit
# Seed checkboxes for roles
Role::AVAILABLE_ROLES.each do |role|
# This adds an unsaved role to the user if it does not exist
@user.roles.build(name: role) unless @user.has_role?(role)
end
end
# PATCH/PUT /users/1
def update
if @user.update(update_params)
redirect_to @user, notice: 'User was successfully updated.'
else
render :edit
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def user_params
params.require(:user).permit(:username, :email, roles_attributes: [:name, :id, :_keep, :_destroy])
end
def update_params
user_params.tap do |o|
# Adds the _delete attribute if the keep checkbox is unchecked
o[:roles_attributes] = o[:roles_attributes].map do |k,h|
attrs = h.merge(_destroy: (h[:_keep] != "1"))
# Don't let the user update the name of an existing Role!
# This would let a malicious user to grant any role.
h.key?(:id) ? attrs.except(:name) : attrs
end
end
end
end
关于ruby-on-rails - Rails 4 + Rolify Gem : User roles aren't being updated via UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31064695/
自从我处理 django 以来已经有一段时间了,我正在处理一些可能有点旧的代码,现在是 Django 1.7。这个堆栈跟踪是关于什么的? 它不是通过 runserver 发生的,而是通过使用应用程序模
出于某种原因,我的表格行间距不正确,只是将新行覆盖在旧行上。我得到的不是 2 行表,而是 1 行表,其中 2 行相互重叠。这是我的代码的相关部分:
我正在尝试使用自定义用户安装 django-registration-redux。 我已将此包含在我的 settings.py 中: AUTH_USER_MODEL = 'app.customUser
我已在 azure 门户中设置了 Azure Frontdoor。我已经创建了 2个前端主机 myappdev1.azurefd.net dev1.myapp.com 后端池具有以下内容 1.Back
我已在 azure 门户中设置了 Azure Frontdoor。我已经创建了 2个前端主机 myappdev1.azurefd.net dev1.myapp.com 后端池具有以下内容 1.Back
使用来自 Django tutorial 的(部分)投票应用程序例如,我正在尝试获取 pytest-django运行。 使用命令 django-admin startproject mysite2 ,
因此,我尝试向 Django 1.10 应用程序添加一些代码,这些代码仅在服务器启动时运行。我从 here 获取了有关如何执行此操作的说明。和 here 。每次我尝试运行 python 控制台时,我都
我在 IntelliJ IDEA 中使用 Kotlin 插件时遇到问题。首先,打开一个kotlin项目,弹出我需要在这个项目中配置kotlin。但是当我从“工具”菜单中这样做时,它会显示“没有可用的配
请注意,我已经查看了这些线程,但它们没有解决我的问题: (过时的信息,请参阅下面的文档)Access Snowflake query results through PowerBI (我希望这能解决我
当 7 个可用列中的任何 3 个包含该行的数据时,我需要包含行。有没有做类似事情的最佳实践?我想出的解决方案似乎很好,只是想知道是否有更可接受的模式?见 here了解详细信息。以下是我要完成的工作的要
babel-preset-es2015已安装,并且可以使用 es6 功能,如下面 let a = 2; . 但无法使用 es6 模块功能 import fs from 'fs'如下: $ babel-
当我尝试将 View 导入 django 应用程序的 apps.py 模块时出现此错误。我正在使用 django 2.2.9、python 3.7.6、django-oscar 2.0.4。我已覆盖结
我正在使用此代码来填充我的数据库: import os def populate(): python_cat = add_cat('Python') add_page(cat=pyth
我已经将我的 django webapp 部署到我的 heroku 服务器并且它工作正常,直到我添加了一个 websocket 连接,该连接在创建模型对象后立即在单独的 url 中显示该模型对象的内容
尝试阅读文档并纠正一些与 sass 相关的问题。向组件添加了“lang=scss”,但不确定如何解决此问题。任何指导都会有所帮助。 rollup v2.19.0 bundles src/main.js
我使用 django 1.10.5 和 celery 4.0.2。我有这样的结构。 -proj -application __init__.py celery.py cel
所以我正在学习这个 javascript 游戏制作教程,但我真的找不到我做了什么才能使我的红色方 block 不显示在屏幕上。事实上,我确实认为这是速度问题,因为当我在控制台中更改对象的速度时,它出现
从 1.8 升级到 django 1.9 时出现此错误。我检查了类似问题的答案,但我认为这不是任何第三方软件包或应用程序的问题。 Traceback (most recent call last):
我从 Django 1.8 升级到 1.9 但遇到了这个问题 django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. s
我有一个功能性 Django 应用程序,在我看来它有许多 Google Text-To-Speech API 调用和数据库读/写。在本地测试时,加载页面大约需要 3 秒,但是当我将应用程序实时部署到
我是一名优秀的程序员,十分优秀!