0

我正在滚动自己的身份验证,我遇到的问题是我的用户的编辑表单。我这里是表格...

#app/views/users/edit.html.erb
<h1>Editing User</h1>
<%= render 'form' %>
<%= link_to 'Show', @user %> |
<%= link_to 'Back', root_path %>

#app/views/users/_form.html.erb
<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% @user.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation %>
  </div>
  <div class="field">
    <%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel']) %><br/>
  </div>
  <div class="actions"><%= f.submit "Sign Up" %></div>
<% end %>

我遇到的问题是,当我显示编辑表单时:user_type 字段不正确。我希望此字段反映当前为当前用户保存的内容,而不是只显示列表中第一个选项的下拉菜单。

4

2 回答 2

0

您想使用:

<%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel'], @user.user_type) %>

或者:

<%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel']), :selected => @user.user_type %>
于 2013-11-07T15:04:03.677 回答
0

options_for_select有第二个参数来预选它...

options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel'], @user.user_type)
于 2013-11-07T15:04:09.800 回答