为什么轨道形式的选择标签不起作用?

时间:2013-10-23 15:17:50

标签: ruby-on-rails forms html-select

我有一个用户注册表:

<%= 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 %>

如果我拿走“f”,则选择标签有效。就在它之前。但是,如果我拿走它,它将不会转到用户对象。如果我把“f。”在,它说:

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

我见过一些类似但却没有帮助的stackoverflow问题。文档简要提到了这一点,但对于像我这样的新手来说,它太复杂了。

2 个答案:

答案 0 :(得分:5)

答案 1 :(得分:3)

select_tag是用于创建<select>元素的基本ActionView助手。使用表单构建器元素时,通常需要构建器的#select方法,例如:

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

表单构建器方法调用select_tag作为构建操作的一部分,但它正确命名元素,以便在提交表单时传回的结果包含在params的一部分中以一种易于更新资源的方式进行哈希。

这假设您的模型当然具有how_did_you_hear属性。