2015-09-18 91 views
1

我在這裏工作的一個簡單的應用程序需要一個設置頁面,因爲我是新的rails我有點迷路了。我想要在一個數據庫中設置一列作爲鍵和一列作爲值。我已經在這裏設置了一些東西,但我無法讓我的表單更新設置。Rails從表單更新ActiveRecord

我想要做的只有一個設置頁面,其中可以更改多個設置。現在所有的設置都將是文本字段。用我目前的代碼即時錯誤「未定義的方法`to_key'#」。所以這裏是一些代碼:

型號:setting.rb

class Setting < ActiveRecord::Base 
    def self.method_missing(method_name, *args, &block) 
    if method_name[-1] == '=' 
     handle_set_setting method_name.to_s.delete("="), args.first 
    else 
     handle_get_setting method_name 
    end 
    end 

    private 

    def self.handle_get_setting(name) 
    if setting = Setting.find_by(key: name.to_s) 
     setting.value unless setting.value.empty? 
    else 
     nil 
    end 
    end 

    def self.handle_set_setting(name, value) 
    if setting = Setting.find_or_create_by(key: name.to_s) 
     setting if setting.update(value: value.to_s) 
    end 
    end 
end 

這是我settings_controller.rb(林100%肯定,我沒有在這裏多的是正確的)

class SettingsController < ApplicationController 
    before_action :set_settings, only: [:edit, :update, :show] 
    def index 
    @settings = Setting.all 
    end 

    def new 
    @settings = Setting.new 
    end 


    def update 
    @settings.update(params) 
    end 

    private 

    def params 
     params.require(:key).permit(:value, :etc) 
    end 
    def set_settings 
     @settings = Setting.find params[:key] 
    end 

end 

和這裏是我的index.html.erb(很肯定這是完全錯誤的)

<h2> Settings </h2> 

<h3> Movie Settings </h3> 
<%= form_for(@settings) do |f| %> 
<%= f.hidden :key => "cp_api" %> 
<%= f.label :value %><br> 
<%= f.text_field :value %> 
<%= f.submit %> 
<% end %> 

以下是錯誤日誌:

Started GET "/settings" for ::1 at 2015-09-18 00:57:24 -0400 
Processing by SettingsController#index as HTML 
    Setting Load (0.1ms) SELECT "settings".* FROM "settings" 
    Rendered settings/index.html.erb within layouts/application (3.6ms) 
Completed 500 Internal Server Error in 7ms (ActiveRecord: 0.1ms) 

ActionView::Template::Error (undefined method `to_key' for # <Setting::ActiveRecord_Relation:0x007fa186b88128>): 
    1: <h2> Settings </h2> 
    2: 
    3: <h3> Settings </h3> 
    4: <%= form_for(@settings) do |f| %> 
    5: <%= f.hidden :key => "cp_api" %> 
    6: <%= f.label :value %><br> 
    7: <%= f.text_field :value %> 
    app/views/settings/index.html.erb:4:in `_app_views_settings_index_html_erb__4580226212883122031_70165864193720' 

我確定它的確很容易弄清楚,但我只是真的迷失在這裏,我一直在這個問題上抨擊我的頭2天,我似乎無法弄清楚。

謝謝!

+0

在哪條線你得到那個錯誤?你可以在問題中發佈錯誤日誌嗎? – Pavan

+0

嘗試將'params.require(:key).permit(:value,:etc)'改爲'params.require(:setting).permit(:value,:etc)' –

+0

@Pavan添加錯誤日誌,錯誤爲在index.html.erb第4行 – djbartos93

回答

1

您需要添加@setting = Setting.Newindex方法如下面

def index 
    @settings = Setting.all 
    @setting = Setting.new 
end 

,改變<%= form_for(@settings) do |f| %>到<%= form_for(@setting) do |f| %>

而且你需要更改params方法如下

def params 
    params.require(:setting).permit(:value, :key) 
end 

而且將<%= f.hidden :key => "cp_api" %>更改爲< %= f.hidden_field :key => "cp_api" %>,以避免進一步的問題

+0

好吧!這使它的大部分工作,但現在我得到 錯誤SystemStackError(堆棧級別太深): app/controllers/settings_controller.rb:33:在'參數' – djbartos93

+0

算了一下!謝謝!!!! – djbartos93

+0

@ djbartos93你能說出是什麼原因導致了這個錯誤? – Pavan