2016-10-15 35 views
0

我的重新編輯有問題。讓我解釋一下:當我編輯和更新一個對象時,當前值在我的show view中是OK的。但是,如果我重新編輯當前值不是最後選擇。當前值在重新編輯後不會保存

問題僅限於複選框或選擇。

我的形式

<%= f.fields_for :situations do |s| %> 

<p><label for="plage">Plage</label> 
<%= s.select :plage?, ["", "oui","non"] %> à <%= s.select :distanceplage?, ["", "moins de 1", "2","3", "4", "5 et plus"] %> km</p> 
<% end %> 

控制器

def edit 

end 

def update 
     @camping = Camping.find(params[:id]) 
     respond_to do |format| 
     if @camping.update(camping_params) 
      format.html { redirect_to @camping, notice: 'Camping was successfully updated.' } 
      format.json { render :show, status: :ok, location: @camping } 
     else 
      format.html { render :edit } 
      format.json { render json: @camping.errors, status: :unprocessable_entity } 
     end 
     end 
    end 

我怎樣才能解決這個問題?謝謝你的幫助 !

+0

我認爲在數據庫中的當前值是好的。你檢查過了嗎?這只是你不用數據庫值預加載選擇器。您創建一個新的空白標籤,而不是... – Maxence

+0

是目前的價值是好的。問題只出現在顯示選擇和複選框上。如果我添加text_field的值是好的...我不知道爲什麼... – nicolaswecandoit

+0

對不起,我不在我的電腦前,但我想我設法用selector_tag和option_for_select選項做到這一點。有人可能會發布完整答案。如果沒有,我明天會回覆給你,告訴你我的解決方案。 – Maxence

回答

0

好了,所以經過一番搜索解決方案是這樣的:

:include_blank => true 

所以我的觀點是這樣的

<p><label for="plage">Plage</label> 
<%= s.select :plage, ["oui","non"], :include_blank => true %> à <%= s.text_field :distanceplage %> km</p> 
相關問題