我想使主要电话号码从rails嵌套的形式。我的模型关联:
# customer.rb
class Customer < ApplicationRecord
has_many :telephones, dependent: :destroy
accepts_nested_attributes_for :telephones, allow_destroy: true
end
# telephone.rb
class Telephone < ApplicationRecord
belongs_to :customer
enum status: {primary: 1, secondary: 0}
end
我的表单:
# _form.html.haml
= form_with(model: customer, local: true) do |form|
....
.....
= form.fields_for :telephones do |builder|
= render 'telephone_fields', form: builder
= link_to_add_fields "Add Telephone", form, :telephone
在我的局部视图telephone_fields中,如下所示:
# _telephone_fields
%fieldset
.form-group
= form.label :content, 'Telephone'
= form.label :status
= form.radio_button :status, "primary"
但是,这是不正确的,因为所有的单选按钮都可以被选中。
也许有人能帮上忙。谢谢!
发布于 2019-03-03 05:59:11
您需要编写JS代码。基本上,给单选按钮一些类。添加侦听器,检查值,如果为某些项目选择了(主要),则为其他项目隐藏。
https://stackoverflow.com/questions/54960264
复制相似问题