我正在滚动自己的身份验证,而我遇到的问题是我的用户的编辑表单。我这是表格......
#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字段不正确。我希望这个字段能够反映当前用户当前保存的内容,而只是通过显示的列表中的第一个选项获得下拉列表。
答案 0 :(得分: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 %>
答案 1 :(得分:0)
options_for_select有第二个参数来预选它......
options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel'], @user.user_type)