2017-08-25 65 views
1

我有一个帮助我们的状态名称和缩写,并且我正在编辑配置文件视图中成功使用此窗体来呈现下拉菜单中的状态并将状态保存到分区时用户选择并提交表格。Rails窗体选择下拉菜单显示存储在数据库中的值

<%= f.label :State %> 
    <%= f.select(:state, options_for_select(us_states), :include_blank => "Please Select") %> 

一旦我的形式保存和恢复,即使状态保存到数据库,表格仍显示“请选择”,所以我已经试过这样:

<%= f.label :State %> 
     <%= f.select(:state, options_for_select(us_states), selected: @clinician_profile.state) %> 

但随后就说明帮助者数组(阿拉巴马州)中的第一个状态,而不是保存的状态。

如何在数据库中没有数值时显示“请选择”,但在用户选择后显示适当的状态?

感谢您的帮助! 这里是我的表单顶部:

<%= form_for(@clinician_profile) do |f| %> 
    <%= render 'shared/error_messages', object: f.object %> 

这里是我的控制器:

class ClinicianProfilesController < ApplicationController 
def edit 
    @clinician_profile = current_user.clinician_profile 
end 

def index 
end 

def show 

end 

def create 
end 

def update 
@clinician_profile = current_user.clinician_profile 
if @clinician_profile.update_attributes(clinician_profile_params) 
    flash[:success] = "Profile Updated" 
    redirect_to edit_clinician_profile_path(current_user) 
else 
    render 'edit' 
    end 
end 


private 

     def clinician_profile_params 
      params.require(:clinician_profile).permit(:address1, :address2, :city, :state, :zip, 
       :accepting_patients, :rate, :license_number, :license_state, :years_licensed, :years_practicing, 
       :school, :year_graduated, :accepts_insurance, :sliding_scale, :bio, :website, insurance_ids: [], race_ids: [], language_ids: []) 

end 
end 

回答

0

使用options_for_selectselect,该selected选项应包括为options_for_select的观点的问题。你也应该使用prompt而不是include_blank所以,你的代码看起来应该像下面

<%= f.select(:state, options_for_select(us_states, selected: @clinician_profile.state), prompt: "Please Select") %> 
相关问题