通过嵌套属性更新关联模型上的多个复选框|茧宝石|滑轨 [英] Update multiple checkboxes on association model through nested attributes | Cocoon gem | Rails

查看:90
本文介绍了通过嵌套属性更新关联模型上的多个复选框|茧宝石|滑轨的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个房屋模型,它有很多house_rules

I have a House model, which has_many house_rules

class House < ActiveRecord::Base
 has_many :house_rules, dependent: :destroy
 accepts_nested_attributes_for :house_rules, reject_if: :all_blank, allow_destroy: true
end

这是house_rule模型

Here is the house_rule model

class HouseRule < ActiveRecord::Base
 belongs_to :house
 enum rule_type: { predefined: 0, user_defined: 1 }
 enum allowed: { no: 0, yes: 1 }
end

这是house_rules表的其他列

create_table "house_rules", force: :cascade do |t|
    t.integer  "house_id"
    t.string   "rule"
    t.integer  "rule_type",  default: 1
    t.integer  "allowed",    default: 0
    t.datetime "created_at",             null: false
    t.datetime "updated_at",             null: false
end

我在数据库中保存了一些预定义的house_rules,默认情况下,allowed列设置为0.例如:

I have some predefined house_rules saved in database with allowed column set to 0 by default.For example:

<HouseRule id: 4, house_id: nil, rule: "Smoking allowed", rule_type: 0, allowed: 0, created_at: "2017-03-26 20:54:09", updated_at: "2017-03-26 20:54:09">

我希望将所有这些house_rules作为新的house表单中的复选框.像这样.

I want to have all these house_rules as check_boxes in the new house form. Like this.

因此,当用户选中带有允许吸烟"标签的复选框时,将创建这样的新记录

So, when users checks the checkbox with label 'Smoking allowed', a new record is created like this

<HouseRule id: 5, house_id: 1, rule: "Smoking allowed", rule_type: 1, allowed: 1, created_at: "2017-03-26 20:54:09", updated_at: "2017-03-26 20:54:09">

rule_type: 1 #user_defined rule
allowed: 1   #smoking is allowed

这是我的新house表单

<%= simple_form_for @house do |f| %>
<div id="house_rules">
  <%=  f.simple_fields_for :house_rules do |house_rule| %>
  <% HouseRule.predefined.each do |rule| %>
  <div>
    <%= house_rule.label :allowed, rule.rule %>
    <%= house_rule.check_box :allowed, {}, 'yes', 'no' %> 
  </div>
  <% end %>
  <% end %>
</div>
<% end %>

上面的代码按预期显示了这些复选框,但没有按预期运行.有人可以指导我如何进行这项工作吗?

The above code shows the checkboxes as expected, but doesn't work as expected. Can someone guide me on how to make this work?

此外,我已经在HousesController中将它们列入了白名单

Also, I have whitelisted these in HousesController

house_rules_attributes: [:id, :allowed, :_destroy])


这是表单为每个复选框生成的代码


This is the code the form produces for each checkbox

<label for="house_house_rules_attributes_0_allowed">Smoking Allowed</label>
 <input name="house[house_rules_attributes][0][allowed]" type="hidden" value="0"><input type="checkbox" value="1" name="house[house_rules_attributes][0][allowed]" id="house_house_rules_attributes_0_allowed">

问题在于,所有复选框的idname属性都是相同的,因此,即使我选中一个复选框,也始终仅选中第一个复选框.

The problem is, the id and name attributes are same for all the checkboxes, so even if I check a checkbox, only the first checkbox is always being checked.

正在为关联房屋创建一个新的house_rule记录,但allowed值仍为0,而不是1.

A new house_rule record is being created for the associated house, but the allowed value is still 0, it's not 1.

这是我的HousesController new action

def new
 @house = House.find(params[:id])
 @house.house_rules.build
end

推荐答案

由于您实际上没有遍历嵌套行,因此无法正确呈现表单,这就是为什么所有行的id为0的原因.

This does not render the form correctly, since you are not actually iterating over nested rows, that is why the id is 0 for all rows.

相反,我会在new动作中为控制器中的house预创建所有预定义规则.如下:

Instead I would precreate all predefined rules for a house in your controller, in the new action. As follows:

def new 
  @house = House.new 
  HouseRule.predefined.each do |hr|
    @house.house_rules.build(rule: hr.rule)
  end 
end 

在您看来,只需照常遍历房屋规则:

And in your view just iterate over the house-rules as usual:

<%=  f.simple_fields_for :house_rules do |house_rule| %>
  <%= house_rule.label :allowed, house_rule.object.rule %>
  <%= house_rule.check_box :allowed, {}, 'yes', 'no' %> 
</end>

这篇关于通过嵌套属性更新关联模型上的多个复选框|茧宝石|滑轨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆