ledermann / rails-settings

Manage settings with Ruby on Rails
MIT License
1k stars 137 forks source link

Dynamic saving settings #19

Closed NicoBijl closed 11 years ago

NicoBijl commented 12 years ago

i am building an app and want to give the users the possibility to change there settings, so i want to save from a form with this code:

params[:settings].each do |key, value|
Settings.instance_variable_set("@#{key}", value)
end

do i need to call a save method?? Settings.save?

Because it wont work like this...

Darep commented 12 years ago

I just did this like this:

params[:settings].each do |key, value|
    Settings[key] = value
end

Works wonderfully!

Svashta commented 11 years ago

I am struggling with making this work in my app. I would like to have a "system-wide" settings page, where administrators could change system settings (not related to users) via some kind of form.

I have created controller with index an update actions, but i can't manage to process what form send in the update action. Currently, i have a problem with UTF-8 and a check mark thing.

would anyone be so kind a help me make this work.

sasharevzin commented 11 years ago

I will send u my controller and view

On 11 בדצמ 2012, at 10:58, Mitja notifications@github.com wrote:

I am struggling with making this work in my app. I would like to have a "system-wide" settings page, where administrators could change system settings (not related to users) via some kind of form.

I have created controller with index an update actions, but i can't manage to process what form send in the update action. Currently, i have a problem with UTF-8 and a check mark thing.

would anyone be so kind a help me make this work.

— Reply to this email directly or view it on GitHub.

Svashta commented 11 years ago

OMG, this was fast. I am looking forward to see how it is done. Thanks a million!

sasharevzin commented 11 years ago

i was driving...

here the code:

view: <%= form_tag(update_settings_path) do %> <% @settings.each do |key, value| %>

<%= label_tag key %>
<%= text_area_tag key, value %>

<% end %>

<%= submit_tag("Save Changes") %>

<% end %>

controller: class SettingsController < ApplicationController def index @settings = Setting.all end def update Setting.all.each do |key, value| eval("Setting.#{key} = params[key]") end redirect_to settings_path, :notice => 'Settings updated' # Redirect to the settings index end end

On Tue, Dec 11, 2012 at 11:04 AM, Mitja notifications@github.com wrote:

OMG, this was fast. I am looking forward to see how it is done. Thanks a million!

— Reply to this email directly or view it on GitHubhttps://github.com/ledermann/rails-settings/issues/19#issuecomment-11235890.

Thanks

Svashta commented 11 years ago

Hey, tnaks for this. Looks very similar to what i was trying. One more question though - how do i have to set my routes? Do I just add resources :settings, or do i have to make a custom route? Maybe a collection? This is a part i just don't get how to do properly.

Thanks!

Svashta commented 11 years ago

I can get update_settings_path to work. Am getting undefined local variable or method `update_setting_path' for #<#Class:0x5386128:0x3abf7c8>

Svashta commented 11 years ago

Ok, now i am having some progress. I have made routes like this:

resources :settings do collection do put :update_all end end

An in form i now have - form_tag(update_all_settings_path, :method => :put)

Form does work now, and something is being processed, but the value is now null. I'll look further into it in the controller.

sasharevzin commented 11 years ago

Send me your view and controller

On 11 בדצמ 2012, at 12:14, Mitja notifications@github.com wrote:

Ok, now i am having some progress. I have made routes like this:

resources :settings do collection do put :update_all end end

An in form i now have - form_tag(update_all_settings_path, :method => :put)

Form does work now, and something is being processed, but the value is now null. I'll look further into it in the controller.

— Reply to this email directly or view it on GitHub.

Svashta commented 11 years ago

view: <%= form_tag(update_all_settings_path, :method => :put) do %> <% @settings.each do |key, value| %>

<%= label_tag key %>
<%= text_area_tag key, value %>
<% end %> 
<div class="actions"> 
    <%= submit_tag("Shrani spremembe") %> 
</div> 

<% end %>

controller:

def index @settings = Settings.all end

def update_all Settings.all.each do |key, value| eval("Settings.#{key} = params[value]") end redirect_to settings_path, :notice => 'Settings updated' end

sasharevzin commented 11 years ago

What's name of your model?

On 11 בדצמ 2012, at 12:25, Mitja notifications@github.com wrote:

view: <%= form_tag(update_all_settings_path, :method => :put) do %> <% @settings.each do |key, value| %>

<%= label_tag key %>

<%= text_area_tag key, value %>

<% end %>

<%= submit_tag("Shrani spremembe") %>

<% end %> controller:

def index @settings = Settings.all end

def update_all Settings.all.each do |key, value| eval("Settings.#{key} = params[value]") end redirect_to settings_path, :notice => 'Settings updated' end

— Reply to this email directly or view it on GitHub.

Svashta commented 11 years ago

I had an impression i don't need it as it is somehow handled by the gem. Seemed weird, but yeah ... don't have it actually.

Svashta commented 11 years ago

Hey,

i have now reverted back to your code completely and added Setting model (class Setting < ActiveRecord::Base). I am now again having update_settings_path undefined local variable or method error.

What's with that. I don't get it anymore.

Svashta commented 11 years ago

Yay. Got it. Finally. Your code + collection in resource for setting with defined put action = win. Thanks!

sasharevzin commented 11 years ago

Cool

Ps. Sorry, I wasn't able to answer u before cuz had a training ;)

On 11 בדצמ 2012, at 21:53, Mitja notifications@github.com wrote:

Yay. Got it. Finally. Your code + collection in resource for setting with defined put action = win. Thanks!

— Reply to this email directly or view it on GitHub.

ledermann commented 11 years ago

Please try release 2.0.0, which is a complete rewrite. Editing settings via forms is much simpler now, because settings are ActiveRecord instances, too.