Skip to content

Instantly share code, notes, and snippets.

@vshvedov
Created January 24, 2011 10:35
Show Gist options
  • Save vshvedov/793051 to your computer and use it in GitHub Desktop.
Save vshvedov/793051 to your computer and use it in GitHub Desktop.
# View
<% form_for :spec do |form| %>
<fieldset>
<legend><span><%= future_i18t("What's your current marital status?") %></span></legend>
<div class="form-input">
<%= error_messages_for'spec' %>
<div class="radio_button_row">
<%= radio_button :spec, :p2_marital_status, 'p2_marital_single_never_married', :onclick =>"toggleOtherExplain;" %>
<%= future_i18t('Single, Never Married') %> <br />
<%= radio_button :spec, :p2_marital_status, 'p2_marital_married', :onclick => "toggleOtherExplain();"%>
<%= future_i18t('Married') %> <br />
<%= radio_button :spec, :p2_marital_status, 'p2_marital_divorced', :onclick => "toggleOtherExplain();" %>
<%= future_i18t('Divorced') %> <br />
<%= radio_button :spec, :p2_marital_status, 'p2_marital_widowed', :onclick => "toggleOtherExplain();"%>
<%= future_i18t('Widowed') %> <br />
<%= radio_button :spec, :p2_marital_status, 'p2_marital_annulled', :onclick => "toggleOtherExplain();"%>
<%= future_i18t('Married Annulled or other') %> <br />
</div>
<div id="sub_question" style="display:none">
<%= text_field_for form, :p2_marital_others, "Explain" %>
</div>
</div>
<%= submit_tag 'Continue', :class => "submit" %>
</fieldset>
<% end %>
<script language="JavaScript" type="text/javascript">
function toggleOtherExplain() {
if ($('spec_p2_marital_status_p2_marital_annulled').checked) {
Element.show('sub_question');
Field.focus('spec_p2_marital_others');
} else {
Element.hide('sub_question');
Field.clear('spec_p2_marital_others');
}
}
toggleOtherExplain();
</script>
# Controller
def marital_status
@last_action = 'marital_status'
@percent_complete = 40
@title = "Information About Your Marital Status"
@ind = "times_married"
@spec.marital_status = true
if param_posted?(:spec)
if params[:spec][:p2_marital_status] == 'p2_marital_single_never_married'
@ind = 'children_count'
end
if params[:spec][:p2_marital_status] == 'p2_marital_annulled'
@spec.marital_status_others = true
end
validate_and_update_form
end
end
# Helper
def validate_and_update_form
params[:spec][:last_action] = @last_action
params[:spec][:last_controller] = @last_controller
if @user.spec.update_attributes(params[:spec])
redirect_to :controller => @con, :action => @ind
end
end
# wraps in div tags and formates "form.text_field", require the parameter for field
# field is the actually name of the field
# has an optional parameter of field_english, eg. if the field name is "p1_pr_fist_name", field_english should be name ¬
# "first_name".
# if you didn't specify size, size will be taken from HTML_TEXT_FIELD_SIZE located in enviroment.rb
# if you didn't specify size, size will be taken from DB_STRING_MAX_LENGTH located in enviroment.rb
# field and field_english used together is for using humanize;
def text_field_for(form, field, field_human='',
size=HTML_TEXT_FIELD_SIZE,
maxlength=DB_STRING_MAX_LENGTH)
# field_human is used to check to see if it is not blank and then translates it on the label
if field_human.blank?
label = content_tag("label", "#{ h field.humanize}: ", :for => field)
elsif field_human == 'leave empty'
label = ''
else
label = content_tag("label", "#{future_i18t( h field_human)}: ", :for => field)
end
# # added a a star to required fields
# if not_required.blank?
# star = content_tag("em", "<img src=\"#{image_path('required_star.gif')}\" alt=\"required\"/>")
# else
# star =''
# end
form_field = form.text_field field, :size => size, :maxlength => maxlength
content_tag("div", "#{label} #{h form_field}", :class => "form_row")
# content_tag("li", "#{label} #{form_field}")
end
def future_i18t(abc)
@abd=abc
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment