0

Is it possible to either extract the text portion of a select tag after it is passed to a controller or do something else crafty with rails to get the value between the <option></option> tags?

My raw html is simple:

<select>
  <option value></option>
  <option value="5">I have a small problem</option>
  <option value="10">I have a big problem</option>
  <option value="15">I have a massive problem</option
</select>

I'm converting the selected value to integers in my controller which triggers other system calls (such as routing to the correct support person), however, I'd like to be able to also save the text portion into the user's profile for tracking (i.e. `user.issue = "I have a small problem").

Shy of creating some hidden fields and using Javascript, is there another way to get or include the text value when it passes to the controller?

Godzilla74
  • 2,358
  • 1
  • 31
  • 67
  • This is extremely easy with javascript, but can't you use `select_tag` ? Look here http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag – Muntasir Alam Sep 01 '16 at 18:25
  • I am using `select_tag`, I was just putting the generated HTML there since it's all the same in the end. – Godzilla74 Sep 01 '16 at 18:27
  • Is it possible/ practical for you to store the option values and text into an array ? – Muntasir Alam Sep 01 '16 at 18:30
  • I'm honestly not sure. I'm just going with what I know at this point... Do you have an example? – Godzilla74 Sep 01 '16 at 18:31
  • Firstly you should probably make an option something like `<%= f.select :desired_attribute, ['option1', 'option2']%>` – Muntasir Alam Sep 01 '16 at 18:36
  • This question might help http://stackoverflow.com/questions/10056397/passing-array-to-a-select-tag, hopefully someone else can help you out, I'm not 100% sure myself – Muntasir Alam Sep 01 '16 at 18:38
  • Probably quite easy, but can you at least post the ERB you are using for your form? – Beartech Sep 01 '16 at 23:22

2 Answers2

0

Here is some advice which is going to be very general since you don't give a lot of specifics in your question. Yes, the HTML should be "the same" no matter what but really it does matter how you create it because that reflects how it integrates into the rest of your app.

I would create a table in the DB called severity with rows something like this:

id | text      | val 
1  | 'small'   | 5
2  | 'big'     | 10
3  | 'massive' | 15

Now instead of doing translations elsewhere like you imply ("I'm converting the selected value to integers in my controller which triggers other system calls") you can use things like:

problem.severity.text
=> 'massive'
problem.severity.val
=> '15'

Using relationships like has_one, belongs_to, etc. you can then leverage these values or words anywhere in your application. Something as simple as:

    <%= f.collection_radio_buttons :user_id, Severity.all, :id, :text %>
Beartech
  • 6,173
  • 1
  • 18
  • 41
0

I should recommend you to use select2 for multiple values selecting, select2-rails is its integration with rails. It's pretty simple. Give it a shot.

Tan Nguyen
  • 3,281
  • 1
  • 18
  • 18