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

时间:2022-11-23 10:13:30

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:

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

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

I've seen some * 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.

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

2 个解决方案

#1


3  

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.:

select_tag是用于创建

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

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

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

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

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

#2


5  

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

因为它是f.select,而不是f.select_tag。请参阅http://guides.rubyonrails.org/form_helpers.html#select-boxes-for-dealing-with-models

#1


3  

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.:

select_tag是用于创建

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

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

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

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

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

#2


5  

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

因为它是f.select,而不是f.select_tag。请参阅http://guides.rubyonrails.org/form_helpers.html#select-boxes-for-dealing-with-models