Philip7899
Philip7899

Reputation: 4677

Why is select tag in rails form not working?

I have a user registration form:

<%= form_for(resource, :as => resource_name, 
        :url => registration_path(resource_name), 
        :id => "employer-form") do |f| %>
    <%= devise_error_messages! %>

    <p><%= f.email_field :email, :autofocus => true, :title => "Enter email", :value =>"Enter Email", :class =>"field" %></p>

    <p><%= f.text_field :profile_name, :autofocus => true, :title => "Enter Username", :value => "Enter Username", :class =>"field" %></p>


    <p class="clearfix">
       <%= f.password_field :password, :title => "Enter Password", :value => "Enter Password", :class => "field field-half" %>
       <%= f.password_field :password_confirmation, :title => "Retype Password", :value => "Retype Password", :class => "field field-half" %>     
    </p>

    <p class="clearfix"> 
       <%= f.text_field :first_name, :title => "Enter First Name", :value => "Enter First Name", :class => "field field-half" %>
       <%= f.text_field :last_name, :title => "Retype Last Name", :value => "Enter Last Name", :class => "field field-half" %>
    </p>

    <p class="clearfix">
       <div class="custom-select">
           <%= f.select_tag(:how_did_you_hear, options_for_select
               ([["Bing", :bing],["College Rep", :college_rep],
               ["Facebook", :facebook],["Google", :google],
               ["Pinterest", :pinterest],["Yahoo", :yahoo],
               ["Twitter", :twitter],["Other", :other]])) %>
       </div>  
    </p>

    <div><%= f.submit "Sign up" %></div>
 <% end %>

The select tag works if I take away the "f." right before it. But if I take that away, it won't go to the user object. If I put the "f." in, it says:

undefined method `select_tag' for #<ActionView::Helpers::FormBuilder:0x007f950db434e0>

I've seen some stackoverflow questions that are similar but don't help. The documentation mentions this briefly but it is too complicated for a newbie like myself to understand.

Upvotes: 0

Views: 2671

Answers (2)

ScottM
ScottM

Reputation: 10414

select_tag is the basic ActionView helper for creating <select> elements. When working with form builder elements, you generally want the builder's #select method instead, e.g.:

<%= f.select :how_did_you_hear, [["Bing", :bing], ...

The form builder method calls select_tag as part of the build operation, but it names the element correctly so that the result that is passed back when the form is submitted is included as part of the params hash in a way that makes it easy to update your resource.

This assumes that your model has a how_did_you_hear attribute, of course.

Upvotes: 3

pdu
pdu

Reputation: 10413

Because it's f.select, not f.select_tag. See http://guides.rubyonrails.org/form_helpers.html#select-boxes-for-dealing-with-models

Upvotes: 5

Related Questions