如何更新has_many:通过与Rails 4中的强参数关联

时间:2022-09-23 08:00:16

I try to update the has_many :through association with strong parameters in Rails 4 without success. This is my model :

我尝试更新has_many:通过与Rails 4中的强参数关联而没有成功。这是我的模特:

User
  has_many :group_owner, :class_name => "Group"
  has_many :group_memberships
  has_many :memberships, :through => :group_memberships, :class_name => "Group"
end

Group
  belongs_to :owner, :class_name => "User", :foreign_key => "user_id"
  has_many :user_members
  has_many :members, :through => :user_members, :class_name => "User"
end 

in the Group view (create/edit)

在组视图中(创建/编辑)

    <%= form_for(:group, :url => {:action => 'update', :group_id => @group.id}) do |f| %>   

        <th>New participant</th>
        <td><%= f.collection_select(:members, @users, :id, :first_name) %></td>

        <%= submit_tag("Update Group") %>
    <% end %>

Then, in the controller, I try to use strong parameters to save the new member in the DB:

然后,在控制器中,我尝试使用强参数将新成员保存在DB中:

def groupe_params
  params.require(:group).permit(:user_id, :group_name, members: [:id])
end

But the user is not saved in the DB, and I don't have any error message. When I try to do

但是用户没有保存在数据库中,我没有任何错误消息。当我尝试做的时候

 group.members << user

via the ruby console, it succeeds.

通过ruby控制台,它成功了。

I'm looking for a solution since few hours already. I've tried to understand the solution from this question (How to use Rails 4 strong parameters with has_many :through association?) but I'm still stuck.

几个小时以来我一直在寻找解决方案。我试图理解这个问题的解决方案(如何使用带有has_many的Rails 4强参数:通过关联?)但我仍然卡住了。

I tried to check what is the value of :members variable in my controller :

我试着检查控制器中的成员变量的值是什么:

 member_id = params[:members]
 flash[:notice] = "member id = #{member_id} !!" 

and it is always empty :

它总是空的:

 member id = !!

But, when I replaced in strong parameters :

但是,当我用强参数替换时:

def group_params
  params.require(:group).permit(:user_id, :group_name, members: [:id])
end

by

def group_params
  params.require(:group).permit(:user_id, :group_name, :members)
end

I get the following error :

我收到以下错误:

undefined method `each' for "1":String

{"utf8"=>"✓",
 "authenticity_token"=>"GxCd==",
 "group"=>{"name"=>"G7",
 "members"=>"1"},
 "commit"=>"Update Group",
 "group_id"=>"4"}

that show me that the :members variable is not empty !

这告诉我:members变量不是空的!

Any help is welcome. Thank you.

欢迎任何帮助。谢谢。

1 个解决方案

#1


0  

According to your incoming parameters, params[:members] does not exist - it's in params[:group][:members]. That's why your attempt to print member_id is not working. I suspect you meant to use group_params[:members]:

根据你的传入参数,params [:members]不存在 - 它在params [:group] [:members]中。这就是你打印member_id的尝试不起作用的原因。我怀疑你打算使用group_params [:members]:

member_id = group_params[:members]
flash[:notice] = "member id = #{member_id} !!"

Your second problem is that members is not a list of objects, so specifying members: [:id] will not work. That expects parameters like

您的第二个问题是成员不是对象列表,因此指定成员:[:id]将不起作用。期望参数如

group: {
  members: [
    {id: 1},
    {id: 2}
  ]
}

You're only passing in a list of IDs, so you want members: [], which expects a list of scalar values the way the question you linked to describes.

您只传递一个ID列表,因此您需要成员:[],它需要一个标量值列表,与您链接的问题描述的方式相同。

Lastly, your has_many association is going to expect an attribute called member_ids, not members. So your final strong parameter definition should be:

最后,你的has_many关联会期望一个名为member_ids的属性,而不是成员。所以你最后的强参数定义应该是:

def group_params
  params.require(:group).permit(:user_id, :group_name, member_ids: [])
end

This will let you set the Group's members via group.member_ids = [1,2,3] from the has_many relationship.

这将允许您通过has_many关系通过group.member_ids = [1,2,3]设置组的成员。


It looks like there might be an issue other than parameters, because your Users have many Groups through group_memberships, but your Groups have many Users through user_members. Unless you have other configuration compensating, you want to be using the same table there with :user_id and :group_id columns to get the relationship working correctly in both directions.

看起来可能存在除参数之外的问题,因为您的用户通过group_membership拥有多个Groups,但您的Groups通过user_members拥有许多用户。除非您有其他配置补偿,否则您希望在其中使用相同的表:user_id和:group_id列,以使关系在两个方向上正常工作。

#1


0  

According to your incoming parameters, params[:members] does not exist - it's in params[:group][:members]. That's why your attempt to print member_id is not working. I suspect you meant to use group_params[:members]:

根据你的传入参数,params [:members]不存在 - 它在params [:group] [:members]中。这就是你打印member_id的尝试不起作用的原因。我怀疑你打算使用group_params [:members]:

member_id = group_params[:members]
flash[:notice] = "member id = #{member_id} !!"

Your second problem is that members is not a list of objects, so specifying members: [:id] will not work. That expects parameters like

您的第二个问题是成员不是对象列表,因此指定成员:[:id]将不起作用。期望参数如

group: {
  members: [
    {id: 1},
    {id: 2}
  ]
}

You're only passing in a list of IDs, so you want members: [], which expects a list of scalar values the way the question you linked to describes.

您只传递一个ID列表,因此您需要成员:[],它需要一个标量值列表,与您链接的问题描述的方式相同。

Lastly, your has_many association is going to expect an attribute called member_ids, not members. So your final strong parameter definition should be:

最后,你的has_many关联会期望一个名为member_ids的属性,而不是成员。所以你最后的强参数定义应该是:

def group_params
  params.require(:group).permit(:user_id, :group_name, member_ids: [])
end

This will let you set the Group's members via group.member_ids = [1,2,3] from the has_many relationship.

这将允许您通过has_many关系通过group.member_ids = [1,2,3]设置组的成员。


It looks like there might be an issue other than parameters, because your Users have many Groups through group_memberships, but your Groups have many Users through user_members. Unless you have other configuration compensating, you want to be using the same table there with :user_id and :group_id columns to get the relationship working correctly in both directions.

看起来可能存在除参数之外的问题,因为您的用户通过group_membership拥有多个Groups,但您的Groups通过user_members拥有许多用户。除非您有其他配置补偿,否则您希望在其中使用相同的表:user_id和:group_id列,以使关系在两个方向上正常工作。