2017-08-25 104 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") %> 
相關問題