我有一个大的多步骤表单,使用Wicked逐步构建单个对象。在我测试表单时,我注意到当我点击表单上的后退按钮(我提供的那个按钮返回上一步)时,单选按钮和选择字段中的值将重新设置为默认选项。文本字段显示用户输入的文本。如果他们在字段中输入数据,我想向用户显示字段值。
我已经想出了如何在我的视图中使用long if语句来完成此操作,但是想要简化它。
<% if @business.business_entity_type.nil? %>
<%= f.select :business_entity_type, options_for_select([["Structure", "0"], ["Sole Proprietorship", "Sole Proprietorship"], ["Partnership", "Partnership"], ["Corporation", "Corporation"]], selected: "0", disabled: "0") %>
<% else %>
<%= f.select :business_entity_type, options_for_select([["Structure", "0"], ["Sole Proprietorship", "Sole Proprietorship"], ["Partnership", "Partnership"], ["Corporation", "Corporation"]], disabled: "0"), :value => @business.business_entity_type %>
<% end %>
当我把它放在三元格式时,我得到一个语法错误。
<%= f.select :business_entity_type, options_for_select([["Structure", "0"], ["Sole Proprietorship", "Sole Proprietorship"], ["Partnership", "Partnership"], ["Corporation", "Corporation"]], <%= @business.business_entity_type.nil? ? selected: "0" : :value => @business.business_entity_type %>, disabled: "0") %>
当我在字段中嵌入if语句时,我也会遇到语法错误。
<%= f.select :business_entity_type, options_for_select([["Structure", "0"], ["Sole Proprietorship", "Sole Proprietorship"], ["Partnership", "Partnership"], ["Corporation", "Corporation"]], disabled: "0", selected: "0"), :value => @business.business_entity_type if @business.business_entity_type.present? %>
如何使用更干净的代码实现相同的目标?
答案 0 :(得分:0)
我认为不是使用:value我可以使用selected:
<%= f.select :business_entity_type, options_for_select([["Structure", "0"], ["Sole Proprietorship", "Sole Proprietorship"], ["Partnership", "Partnership"], ["Corporation", "Corporation"]], disabled: "0", selected: ( @business.business_entity_type.nil? ? "0" : @business.business_entity_type)) %>