Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I handle person height within a simple_form?

I have a Profile model, that has a height attribute.

I need to store the height of users (aka profiles), but I am not quite sure how to do it. I assume the best way to do it is to have a select dropdown broken down in inches (i.e. 4'0", 4'1"...6'5", etc.). But I am not quite sure the best way to store this in the db.

I would assume it is simplest to just store inches, but if I do how do I present the height in my select in the format I specified above.

Right now, I just have an integer field:

<%= f.input_field :height, placeholder: "176", class: 'col-lg-9 form-control' %>
like image 620
marcamillion Avatar asked Sep 26 '16 06:09

marcamillion


2 Answers

You can use a number field for this. When saving the users height or updating a user height you can provide an option for the user either choosing it in inches or cms. You can either use javascript or jquery to convert to appropriate units before saving or you can use a before filter for updating and creating operations.

You can do the following instead of text_field and you can even set min and max values for height.

<%= f.number_field :height, placeholder: "176", class: 'col-lg-9 form-control' %>

like image 99
Aditya Varma Avatar answered Sep 19 '22 19:09

Aditya Varma


1.Change hieght variable data type to float as centimeters.

2.Display drop down of foot and inches and convert it into centimeters before save and viceversa before display stored data.

in your view:

<select name=profile[height]>height
<option>4.1</option>
<option>4.2</option>
<option>4.3</option>
<option>4.4</option>
</select>

in your create action of controller:

@profile = Profile.new(profile_params)
@profile.height = @profile.height*30.48 #convert foot to centimeters
@profile.save
like image 21
srihari sairam Ventrapragada Avatar answered Sep 18 '22 19:09

srihari sairam Ventrapragada