Silva_PT_SCP
Silva_PT_SCP

Reputation: 567

Problems using options_for_select helper in rails

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

Upvotes: 0

Views: 127

Answers (1)

blelump
blelump

Reputation: 3243

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.

Upvotes: 1

Related Questions