Sandip の助けのおかげで、なんとか問題を解決できました!
ビューは次のとおりです。
= f.radio_button :system, "bacteria"
Bacteria
= f.radio_button :system, "mammalian"
Mammalian
= f.radio_button :system, "yeast"
Yeast
= f.radio_button :system, "insect"
Insect
%br/
= f.radio_button :system, nil, id: 'other_system_radio',
checked: radio_checked?('system', f.object.system)
Other:
%input.input-small#other_system_text{ value: text_input?('system', f.object.system) }
ヘルパー関数を使用して編集フォームを管理します (値が指定されたものと異なる場合はテキスト フィールドに入力します)。
def radio_checked?(type,val)
case type
when 'system'
['bacteria', 'mammalian', 'yeast', 'insect'].include?(val) ? '' : 'checked'
end
end
def text_input?(type,val)
case type
when 'system'
['bacteria', 'mammalian', 'yeast', 'insect'].include?(val) ? '' : val
end
end
そして、ユーザーがテキスト フィールドにフォーカスしているときに [その他] ラジオ ボタンを選択するための Javascript のビット:
@handle_other_field = ->
$('#other_system_text').focus( -> $('#other_system_radio').attr('checked','checked'))
$('#other_system_text').keyup( -> $('#other_system_radio').val($(this).val()))