Problems using options_for_select helper in rails

124 views Asked by At

I'm trying to get a value from a select box in order to pass the value to the controller but after many attempts I end up with no solution.

Im have this:

<div class="field">
    <%= f.label :activityType %><br>
    <%= select_tag(:activityType, options_for_select([
                                                      ["Cycling", "Cycling"], 
                                                      ["Running", "Running"],                     
                                                      ["Swimming", "Swimming"], 
                                                      ["Gymn", "Gymn"]
                                                     ])) %>
</div>

What I'm I doing wrong? In the documentation they do the same ..

The problem is that my validation for presence = true say that the field is blank.

Thank you

1

There are 1 answers

3
blelump On BEST ANSWER

You're using f.label so probably you also would like to use f.select instead select_tag. The main difference between them is that

select_tag(:activityType, options_for_select([[]]) )

gives exactly activityType param name, so it's accessible in controller via params[:activityType], whereas:

f.select(:activityType, options_for_select([[]]) )

gives params[:object_name][:activityType]. So that, if you're creating an object via params[:object_name], activityType is empty.

Note I'm using :object_name because I don't know your form object name.