html tag を使用してビューでユーザーにグループを割り当てる必要がありますselect
。
#model
class User < ActiveRecord::Base
has_and_belongs_to_many :groups
attr_accessible :groups, #......
end
class Group < ActiveRecord::Base
has_and_belongs_to_many :users
end
#controller
def new
@user = User.new
@groups = Group.all.map{|x| [x.name, x.id]}
end
def create
@user = User.new params[:user]
# @user.groups
if @user.save
flash[:success] = 'ok'
else
render action: 'new'
end
end
#view
= form_for @user, url: {action: 'create'} do |f|
= f.label :group
= f.select :groups, @groups
そしてポストの部分params
{ ....
"groups"=>"1"
...
}
今言っているのは"undefined method
、"1":String"` に対して each' ということです。どうすればそれを取り除くことができますか?