Rails 3,如何在组#create 操作中设置 user.role
我有一个 groups#new
视图。当 current_user 登录时,他们可以创建组。
我的组#新视图:当前用户的隐藏字段工作正常,但我需要将角色 = groupleader 分配给当前用户。
<div>
<%= fields_for :membership do %>
<%= hidden_field_tag "user_id", current_user.id %>
<%= hidden_field_tag "role", "groupleader" %>
<% end %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
然后在我的 groups#create 操作中:
def create
@group = Group.new(params[:group])
@membership = current_user.memberships.build(:group_id => params[:group_id])
这是建立成员资格但不影响角色。
我的模型如下:
class Group < ActiveRecord::Base
has_many :memberships, :dependent => :destroy
has_many :users, :through => :memberships
accepts_nested_attributes_for :users
class User < ActiveRecord::Base #Built with Devise Gem
has_and_belongs_to_many :roles
has_many :memberships, :dependent => :destroy
has_many :groups, :through => :memberships
class Membership < ActiveRecord::Base
attr_accessible :user_id, :group_id
belongs_to :user
belongs_to :group
end
class Role < ActiveRecord::Base
has_and_belongs_to_many :users
end
---编辑 这就是我正在玩的......因为我通过表单发送角色参数,
@membership = current_user.memberships.build(:group_id => params[:group_id], :role => params[:role])
这就是日志中的内容:
Parameters: {"group"=>{"title"=>"Test group w/leader", "description"=>"test"}, "commit"=>"Create Group", "role"=>"groupleader", "authenticity_token"=>"+GCK8bJddObwKdCoiqI5QzlGRyuci8b5JxmgXbOjgsc=", "utf8"=>"✓", "user_id"=>"8"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 8 LIMIT 1
WARNING: Can't mass-assign protected attributes: role
显然,我有一个批量分配问题。
I have a groups#new
view. When the current_user is logged in, they are allowed to create a group.
My Groups#new view: the hidden field for the current user works fine, but I need to assign the role = groupleader to this current user.
<div>
<%= fields_for :membership do %>
<%= hidden_field_tag "user_id", current_user.id %>
<%= hidden_field_tag "role", "groupleader" %>
<% end %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Then in my groups#create action:
def create
@group = Group.new(params[:group])
@membership = current_user.memberships.build(:group_id => params[:group_id])
This is building a membership but not affecting the role.
My models are as follows:
class Group < ActiveRecord::Base
has_many :memberships, :dependent => :destroy
has_many :users, :through => :memberships
accepts_nested_attributes_for :users
class User < ActiveRecord::Base #Built with Devise Gem
has_and_belongs_to_many :roles
has_many :memberships, :dependent => :destroy
has_many :groups, :through => :memberships
class Membership < ActiveRecord::Base
attr_accessible :user_id, :group_id
belongs_to :user
belongs_to :group
end
class Role < ActiveRecord::Base
has_and_belongs_to_many :users
end
---EDIT
Here is what I am playing with.... since I am sending a role parameter through the form,
@membership = current_user.memberships.build(:group_id => params[:group_id], :role => params[:role])
This is whats going through the log:
Parameters: {"group"=>{"title"=>"Test group w/leader", "description"=>"test"}, "commit"=>"Create Group", "role"=>"groupleader", "authenticity_token"=>"+GCK8bJddObwKdCoiqI5QzlGRyuci8b5JxmgXbOjgsc=", "utf8"=>"✓", "user_id"=>"8"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 8 LIMIT 1
WARNING: Can't mass-assign protected attributes: role
Obviously, I have a mass-assign issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的会员资格模型没有 groupleader 的 attr_accessible。难道这就是我无法正常工作的原因吗?
我还建议将其命名为 group_leader_id
Your membership model doesn't have attr_accessible for groupleader. Can't this me the reason why is it not working.
Also I suggest naming it as group_leader_id