复选框正在获取但未放置多对多角色
我正在尝试设置一个 Rails 3 应用程序来使用 Devise 和 CanCan 处理用户角色。
我的关系如下
class User < ActiveRecord::Base
has_many :users_roles
has_many :roles, :through => :users_roles
end
class Role < ActiveRecord::Base
has_many :users_roles
has_many :users, :through => :users_roles
end
class UsersRole < ActiveRecord::Base
belongs_to :user
belongs_to :role
end
实际上一切正常。 Devise 完美地处理了身份验证。 CanCan正在根据capability.rb限制用户行为。
但我在设置 UsersRole 时遇到了一个荒谬的问题。
我在用户编辑页面上定义了复选框,如下所示。
<% for role in Role.all %>
<%= check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role) %>
<%=h role.name.camelize %>
<% end %>
<%= hidden_field_tag "user[role_ids][]", "" %>
如果我通过控制台创建 UserRole,则会根据用户角色选中这些复选框。
但我无法使用这些复选框设置或更改角色!
我已经用这个方法到处找了——语法的变化,切换到 HABTM 和 Roles_mask 方法,重建了我的模型和控制器好几次——但都没有效果。
实际上我的问题的标题并不完全正确 - 复选框正在放置
_method put
authenticity_token XGl6s1iyXJfahdgftc3df8q1ZeehMVzs3LxiQH98jGw=
commit Update
user[current_password] password
user[email] [email protected]
user[name] User Name
user[password]
user[password_confirmatio...
user[role_ids][] 1
user[role_ids][] 4
user[role_ids][]
utf8 ✓
但这些值没有在数据库中设置。
我做错了什么???!!!
I'm trying to set up a Rails 3 app to handle user roles with Devise and CanCan.
My relationships are as follows
class User < ActiveRecord::Base
has_many :users_roles
has_many :roles, :through => :users_roles
end
class Role < ActiveRecord::Base
has_many :users_roles
has_many :users, :through => :users_roles
end
class UsersRole < ActiveRecord::Base
belongs_to :user
belongs_to :role
end
Actually everything is working fine. Devise is handling the authentication perfectly. CanCan is restricting user behaviour based on ability.rb.
But I have a ridiculous problem with setting a UsersRole.
I have defined checkboxes on the User edit page like so.
<% for role in Role.all %>
<%= check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role) %>
<%=h role.name.camelize %>
<% end %>
<%= hidden_field_tag "user[role_ids][]", "" %>
If I create a UserRole via the console, then these checkboxes are checked according to the users role.
But I cannot set or change roles using these checkboxes!
I've been all around the houses with this — variations of syntax, switched to a HABTM and roles_mask approach, rebuilt my models and controllers several times — all to no effect.
Actually the title of my question is not entirely correct - the checkboxes are putting
_method put
authenticity_token XGl6s1iyXJfahdgftc3df8q1ZeehMVzs3LxiQH98jGw=
commit Update
user[current_password] password
user[email] [email protected]
user[name] User Name
user[password]
user[password_confirmatio...
user[role_ids][] 1
user[role_ids][] 4
user[role_ids][]
utf8 ✓
But these values are not being set in the database.
What am I doing wrong??!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,您已在用户模型中指定了 attr_accesible,并且 role_ids 未在那里列出(也不应该列出)。
如果将其与控制器中的 update_attributes 调用结合使用,则永远不会正确设置 role_ids 。
如果是这种情况,那么您应该能够在更新或保存之前手动在控制器中设置 role_ids ,如下所示:
当然,我不确定是否是这种情况,因为您没有包含控制器代码或任何详细信息用户模型。
编辑:
相反,如果在控制器中执行 @user.update_attributes,您可以将其更改为以下内容:
My guess is that you have specified attr_accesible in your User model and that role_ids is not listed there (and it should not be)
If that is combined with an update_attributes call in your Controller then role_ids will never be set properly.
If this is the case then you should manually be able to set the role_ids in your Controller like this before you update or save:
Of course, I'm not certain this is the case since you did not include your controller code or any details with the User model.
Edit:
Instead if doing the @user.update_attributes in the Controller, you could change it to the following instead: