【发布时间】:2015-03-13 08:57:35
【问题描述】:
我有一个带有candidate_name 字段的模型,它在表单上显示为单选按钮的集合。其中一个单选按钮是显示candidate_name_other 虚拟属性的“其他”选项。在candidate_name_other 中输入一个值时,我想将该值存储在数据库的candidate_name 字段中。
我几乎可以让它工作,但是在编辑记录时,当填充candidate_name_other 时,我无法正确选择candidate_name 收音机。
型号:
class Question < ActiveRecord::Base
attr_accessor :candidate_name_other
def candidate_name_other=(value)
self.candidate_name = value if candidate_name == 'other'
end
def candidate_name_other
candidate_name
end
end
表单域:
<%= f.collection_radio_buttons(:candidate_name, [['candidate', 'Candidates'], ['option', 'Options'], ['other', 'Other']], :first, :second) do |b| %>
<% b.label do %>
<%= b.radio_button %><%= b.text %>
<% end %>
<% end %>
<%= f.text_field :candidate_name_other %>
所以当我选择了“其他”单选,输入了一个替代值,保存并加载了编辑操作时,我希望看到这个:
但我实际看到的是这样的:
这是有道理的,因为为了将收音机选为“其他”,该值需要是“其他”而不是“foobar”。如何将正确的“其他”值发送到candidate_name 进行显示?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4