2016-07-30 9 views
0

我有一個設置控制器的設置是這樣的:的Rails的form_tag不顯示現有的字段值

class Admin::SettingsController < ApplicationController 

    def index 
    @settings = Setting.all 
    end 

    def update 
    setting_params.each do |key, value| 
     Setting.where(key: key).first.update_attribute :value, value 
    end 

    redirect_to admin_settings_path, notice: "Settings saved." 
    end 

    private 

    def setting_params 
    params.require(:settings).permit(:site_title, :site_desc) 
    end 

end 

index操作具有相關路徑的視圖文件,它的代碼如下:

<h1>Settings</h1> 

<%= form_tag admin_settings_path, method: "put" do %> 

    <p> 
     <label>Site Title:</label> 
     <%= text_field_tag "settings[site_title]" %> 
    </p> 

    <p> 
     <label>Site Description:</label> 
     <%= text_field_tag "settings[site_desc]" %> 
    </p> 

    <p> 
     <%= submit_tag "Save settings" %> 
    </p> 

<% end %> 

現在,我可以在相關模型中保存/更新這些字段數據,並且我可以在rails控制檯中通過命令Setting.all查看數據。但問題是,當我重新加載設置索引頁時,通過表單保存記錄後,表單是空白的,並且沒有在字段中顯示以前保存的值。

我在做什麼錯在這裏?

回答

0

我認爲你需要使用form_for(@variable)而不是僅僅是一個form_tag,因爲你需要在保存後持久化對象。

的form_for與資源工作的,看看這裏:http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html

+0

我們不能創造價值與助手的form_tag領域的執着。其次,我試着用@setting變量使用form_for helper,但它說「表單中的第一個參數不能包含零或爲空」。 –