Я пытаюсь установить отношение «многие ко многим», но получаю :roles=>[{:error=>:invalid}]
при попытке сохранить.
Модели
class User < ApplicationRecord
has_many :user_roles, dependent: :destroy
has_many :roles, through: :user_roles
end
class Role < ApplicationRecord
has_many :children, :class_name => "Admin::Role", foreign_key: "parent_id"
belongs_to :parent, :class_name => "Admin::Role"
has_many :user_roles, dependent: :destroy
has_many :users, through: :user_roles
end
class Admin::UserRole < ApplicationRecord
belongs_to :user
belongs_to :role
end
Контроллер
def new
@user = User.new
end
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
redirect_to users_url
end
end
end
def user_params
params.require(:user).permit(:role_ids => [])
end
_form.html.erb
form_with(model: user) do |form|
form.collection_select(:role_ids, Roles.all, :id, :title, multiple: "multiple")
end
Журнал параметров
"user"=>{"role_ids"=>["", "1"]}