lundi 2 mars 2015

Show field value if it isn't nil in rails form

I have a large multistep form that builds a single object step by step using Wicked. As I've been testing the form, I notice when I hit the back button on the form (the one I provide to go back to the previous step), the values in radio buttons and in select fields are set back to the default option. The text fields show the text that the user entered in. I would like to show the field value to the user if they entered data into the field.


I have figured out how to do this with a long if statement in my view, but would like to refactor it.



<% 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 %>


How would I achieve the same objective with more clean code?


Aucun commentaire:

Enregistrer un commentaire