だから私はスイッチを使用するために、下にレールsimple_formと、この入力を使用しています:変更スイッチ値「0」/「1」の文字列へのRails simple_form
class SwitchInput < SimpleForm::Inputs::Base
def input
input_html_classes.unshift('fg-line')
template.content_tag(:div, class: 'toggle-switch') do
template.concat @builder.check_box(attribute_name, input_html_options)
template.concat inline_label
end
end
def inline_label
template.content_tag(:label, class: 'ts-helper') do
template.concat options[:inline_label]
end
end
end
私が作成しようとしていますが入力値を文字列として保存するスイッチ。左(デフォルト)に切り替えると保存される値は「小」になり、スイッチを右に回すと保存される値は「大」になります。
今私はbefore_save
コールバックを経由して「小さい」/「大」として値を保存しています:
private def convert_switch_value
if size == '0'
self.size = 'small'
else
self.size = 'large'
end
return true
end
ユーザーがフォームに戻ったときに、保存された値ではないので、これには問題がありますスイッチで表示されます。スイッチはデータベースに保存されている値に関係なく常にオフになっています。
simple_formを使用して文字列値をデータベースに保存し、フォームに再入力するときに保存された値を表示する方法はありますか?
ありがとうございます!