Rails 3,如何在组#create 操作中设置 user.role

发布于 2024-12-10 09:19:03 字数 2132 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

农村范ル 2024-12-17 09:19:03

您的会员资格模型没有 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文