ruby - How to update has_many :through association with strong parameters in Rails 4 -


i try update has_many :through association strong parameters in rails 4 without success. model :

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 group view (create/edit)

    <%= form_for(:group, :url => {:action => 'update', :group_id => @group.id}) |f| %>             <th>new participant</th>         <td><%= f.collection_select(:members, @users, :id, :first_name) %></td>          <%= submit_tag("update group") %>     <% end %> 

then, in controller, try use strong parameters save new member in db:

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

but user not saved in db, , don't have error message. when try

 group.members << user 

via ruby console, succeeds.

i'm looking solution since few hours already. i've tried understand solution question (how use rails 4 strong parameters has_many :through association?) i'm still stuck.

i tried check value of :members variable in controller :

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

and empty :

 member id = !! 

but, when 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 following error :

undefined method `each' "1":string  {"utf8"=>"✓",  "authenticity_token"=>"gxcd==",  "group"=>{"name"=>"g7",  "members"=>"1"},  "commit"=>"update group",  "group_id"=>"4"} 

that show me :members variable not empty !

any welcome. thank you.

according incoming parameters, params[:members] not exist - it's in params[:group][:members]. that's why attempt print member_id not working. suspect meant use group_params[:members]:

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

your second problem members not list of objects, specifying members: [:id] not work. expects parameters like

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

you're passing in list of ids, want members: [], expects list of scalar values way the question linked to describes.

lastly, has_many association going expect attribute called member_ids, not members. final strong parameter definition should be:

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

this let set group's members via group.member_ids = [1,2,3] has_many relationship.


it looks there might issue other parameters, because users have many groups through group_memberships, groups have many users through user_members. unless have other configuration compensating, want using same table there :user_id , :group_id columns relationship working correctly in both directions.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -