There is the following code of controller:
def create
@employee = business.beauty_salon_employees.build(employee_params)
if @employee.save
#
else
render 'new'
end
end
def edit
@employee = business.beauty_salon_employees.find(params[:id])
end
def update
@employee = business.beauty_salon_employees.find(params[:id])
if @employee.update(employee_params)
#
else
render 'edit'
end
end
protected
def employee_params
params.require(:beauty_salon_employee).permit(:name, :description, :phone, { beauty_salon_service_ids: [] })
end
And the following code of view:
- @business.beauty_salon_services.each do |s|
.row
= check_box_tag "beauty_salon_employee[beauty_salon_service_ids][]", s.id, @employee.beauty_salon_services.include?(s)
= s.name
Models code:
has_many :beauty_salon_employee_services, dependent: :destroy
has_many :beauty_salon_services, through: :beauty_salon_employee_services
accepts_nested_attributes_for :beauty_salon_services
And
has_many :beauty_salon_employee_services, dependent: :destroy
has_many :beauty_salon_employees, through: :beauty_salon_employee_services
Problem is the following thing: when I create some BeautySalonEmployee with some checkboxes count checked (for example, 1) all is good. But when I try to update this record with no checkboxes checked I get no errors, but nothing is updated, ie when I open this record again I still see 1 checkbox checked. How can I fix it? What's wrong?
If check boxes aren't checked they aren't sent.
Just add a hidden field with the same name set to nil eg
= hidden_field_tag 'beauty_salon_employee[beauty_salon_service_ids][]', nil
This will be sent if none of the checkboxes are checked but if they are they will be sent instead
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.