0

I am rolling my own authentication, and the issue I am running into is the edit form for my users. I here is the form...

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

The issue I am having is that when I display the edit form the :user_type field is not correct. I want this field to reflect what is currently saved for the current user, instead I just get the drop down with the first option in the list displayed.

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には、事前に選択するための2番目のパラメーターがあります...

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