如何在select标签中将默认的'selected'选项设为嵌套属性的参数?

时间:2017-04-21 12:42:58

标签: ruby-on-rails ruby-on-rails-5

我试图使其成为表单中选择字段中显示的默认值是用户参数,但问题是参数是嵌套属性。我不确定如何去做,到目前为止我还没有通过谷歌找到任何东西。

这就是我所拥有的:

<%= f.select :gender, options_for_select(['Male', 'Female']), { prompt: '--select gender--', selected: params[profile: [:gender]] }, { class: 'form-control' } %>

为了澄清,ProfileUser的嵌套属性。他们有一对一的关联(Profile属于User)。

我也尝试过放selected: params[:gender],但这似乎也不起作用。

以下是user_params(在用户控制器中):

def user_params
      params.require(:user).permit(
        :email, :password, :password_confirmation,
        profile_attributes: [
            :user_id, :first_name, :last_name, :gender, :date_of_birth,
            :occupation, :education, :city, :post_code, :country
        ]
      )
end

编辑和更新方法(也是用户控制器):

# Edit profile page
  def edit
    @user = User.find(params[:id])
  end

  # Patch method for updating profile
  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

以下是表单所在的视图,users / edit.html.erb

<% provide(:title, 'Edit profile') %>
<section>
  <h3 class="pink-text"><i class="fa fa-pencil"></i> EDIT PROFILE</h3>
  <%= render 'form' %>
</section>

用户/ _form.html.erb:

<%= form_for(@user) do |f| %>
  <%= render 'shared/error_messages' %>
    <div class="form-group">
      <h4>Account information</h4>
      <%= f.label :email, class: 'col-sm-2 control-label' %>
      <div class="col-sm-10">
        <%= f.email_field :email, class: 'form-control', placeholder: "john@example.com" %>
      </div>
      <%= f.label :password, class: 'col-sm-2 control-label' %>
      <div class="col-sm-10">
        <%= f.password_field :password, class: 'form-control', placeholder: "A-Z,a-z,0-9" %>
      </div>
      <%= f.label :password_confirmation, "Confirm password", class: 'col-sm-2 control-label' %>
      <div class="col-sm-10">
        <%= f.password_field :password_confirmation, class: 'form-control', placeholder: "Confirm password" %>
      </div>
    </div>

    <!-- Render profile form -->
    <%= f.fields_for :profile, @user.profile do |profile| %>
      <% render 'profile_fields', f: profile %>
    <% end %>

  <%= f.submit "Submit", class: "btn btn-primary" %>
<% end %> <!-- form_for @user -->

和users / _profile_fields.html.erb

<div class="form-group">
  <%= f.label :first_name, class: 'col-sm-2 control-label' %>
  <div class="col-sm-10">
    <%= f.text_field :first_name, class: 'form-control', placeholder: 'First Name' %>
  </div>

  <%= f.label :last_name, class: 'col-sm-2 control-label' %>
  <div class="col-sm-10">
    <%= f.text_field :last_name, class: 'form-control', placeholder: 'Last Name' %>
  </div>

  <%= f.label :gender, class: 'col-sm-2 control-label' %>
  <div class="col-sm-10">
    <%= f.select :gender, options_for_select(['Male', 'Female']), { prompt: '--select gender--', selected: @selected }, { class: 'form-control' } %>
  </div>

  <%= f.label :date_of_birth, class: 'col-sm-2 control-label' %>
  <div class="col-sm-10">
    <%= f.date_field :date_of_birth, class: 'form-control' %>
  </div>

  <%= f.label :occupation, class: 'col-sm-2 control-label' %>
  <div class="col-sm-10">
    <%= f.text_field :occupation, class: 'form-control', placeholder: "Occupation" %>
  </div>

   <%= f.label :city, class: 'col-sm-2 control-label' %>
   <div class="col-sm-10">
     <%= f.text_field :city, class: 'form-control', placeholder: "City" %>
   </div>

   <%= f.label :post_code, class: 'col-sm-2 control-label' %>
   <div class="col-sm-10">
     <%= f.text_field :post_code, class: 'form-control', placeholder: "Post code" %>
   </div>

    <%= f.label :country, class: 'col-sm-2 control-label' %>
    <div class="col-sm-10">
      <%= f.country_select :country, { priority_countries: ["AU", "US", "GB", "CA", "NZ"], include_blank: true }, { class: 'form-control' } %>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

不要从您的视图中调用params,首先设置要在控制器中显示的值(使用类实例变量),然后在视图中使用它。

在控制器中:

def edit
  @user = User.find(params[:id])
  @selected = user_params[:profile][:gender]
end

def user_params
  params.require(:user).permit(
    :email, :password, :password_confirmation,
    profile: [
      :user_id, :first_name, :last_name, :gender, :date_of_birth,
      :occupation, :education, :city, :post_code, :country
    ])
end

请注意,user_params也已更新(已将profile_attributes更改为profile)。

在视图中:

<%= f.select :gender, options_for_select(['Male', 'Female']), { prompt: '--select gender--', selected: @selected }, { class: 'form-control' } %>