Reputation: 25
Im working on a simple app here that needs a settings page since I am new to rails I am a little lost here. I would like to get it setup in a database with one column as a key and one column as the value. I already have a few things setup here but I am having trouble getting my forms to update the settings working.
All I want to do is have a single settings page with multiple settings that can be changed in it. for now all settings are going to be text fields. With my current code im getting the error "undefined method `to_key' for #". so here is some code:
model: 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
Here is my settings_controller.rb (Im 100% sure I dont have much correct in here.)
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
and here is my index.html.erb (pretty sure this is all wrong)
<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 %>
Here is the error log:
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'
Im sure its something really easy to figure out, but I'm just really lost here, Ive been bashing my head for 2 days on this problem and I just cant seem to figure it out.
Thanks!
Upvotes: 2
Views: 243
Reputation: 33542
You need to add @setting = Setting.New
to your index
method like below
def index
@settings = Setting.all
@setting = Setting.new
end
and change <%= form_for(@settings) do |f| %
> to <%= form_for(@setting) do |f| %>
Also you need to change your params
method to below
def params
params.require(:setting).permit(:value, :key)
end
And change <%= f.hidden :key => "cp_api" %>
to <%= f.hidden_field :key => "cp_api" %>
to avoid further issues
Upvotes: 2