ruby on rails - How to set a default value of radio_buttons in simple-form -
i going set default value radio_buttons in simple_form. code:
<%= f.input :library_type, as: :radio_buttons, collection: library.library_types.collect { |k,_| [k.capitalize, k]} , checked: 'custom’%>
class library < activerecord::base enum library_type: [:standard, :custom] ... end 2.2.3 :002 > library.library_types.collect { |k,_| [k.capitalize, k]} => [["standard", "standard"], ["custom", "custom"]] i added option checked: ‘custom’. when creating new library, custom default selected.
however, cause error when user choose library_type. when user edit library, select custom well, if user selected standard.
anyone know how solve this? thanks.
i move logic controller. in new action have set library_type field custom, , you. like
class librariescontroller < applicationcontroller def new @library = library.new(library_type: 'custom') render 'edit' end def create ... end def edit #find @library here render 'edit' end def update ... end end so, set library_type custom new instances , not overwrite created records.
Comments
Post a Comment