I'm new to the Ruby on Rails environment, so I am stuck on what might be a simple question: I'm looking to define some text strings/labels that correspond to a numeric value. These values will be stored in the database and then used in my code instead of the numeric values.
In C, I would to something like this:
#define Accounting 0
#define Engineering 1
#define Education 2
...to be used like this:
if (field_of_study == Accounting) ...
I want to be able to do this in Rails controllers/views. I currently have to do something like this in my views to display items:
<tr>
<td><%= link_to user.name, user %></td>
<% if user.studyField == 0 %>
<td>Accounting</td>
<% elsif user.studyField == 1 %>
<td>Engineering</td>
<% elsif user.studyField == 2 %>
<td>Education</td>
<% end %>
</tr>
I would also like to use the text strings/labels in a drop-down menu in the form_for
form and then save it using the numeric identifier. Do I need a before_save
method to translate the two or is their an automatic way of doing this?