From d0f5e351009fa12fc24da6485e6db326c61496c6 Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Thu, 15 Sep 2011 09:20:50 +0400 Subject: [PATCH] Settings save --- app/assets/javascripts/page.coffee | 1 - app/assets/javascripts/settings.coffee | 7 +++++-- app/controllers/user_controller.rb | 20 ++++++++++++-------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/page.coffee b/app/assets/javascripts/page.coffee index 8e7504d..c1ee9bc 100644 --- a/app/assets/javascripts/page.coffee +++ b/app/assets/javascripts/page.coffee @@ -39,6 +39,5 @@ $ -> $(this).parent('li').toggleClass('open') false $('.dropdown-menu li a').live 'click', -> - console.log(1) $(this).parent().parent().parent().toggleClass('open') false \ No newline at end of file diff --git a/app/assets/javascripts/settings.coffee b/app/assets/javascripts/settings.coffee index d3c449a..1c56346 100644 --- a/app/assets/javascripts/settings.coffee +++ b/app/assets/javascripts/settings.coffee @@ -49,8 +49,11 @@ $('#settings-forms form').live 'submit', -> params = {} $(this).find('input, select').each -> if $(this).attr('name') - params[$(this).attr('name')] = $(this).val() - console.log(params) + if $(this).attr('type') is 'checkbox' + params[$(this).attr('name')] = ($(this).attr('checked') is 'checked') + else + params[$(this).attr('name')] = $(this).val() + _session.query '/user/update/', { params: params } false $('#settings-forms input[type$="submit"]').live 'mouseup', -> diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 5f52f2e..f6e8fd6 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -40,19 +40,23 @@ class UserController < ApplicationController def update return unless authorized? + allowed_params = [:name, :email, :lang, :show] @res = {} user = User.find_by_vkid(params[:mid]) - - render :json => request - return - - unless params[:username].nil? or params[:email].nil? - user.name = params[:username] - user.email = params[:email] - user.lang = params[:lang] if ['ru', 'en'].include? params[:lang] + update_params = {} + params[:params] = params[:params].each{ |k, v| update_params[k.to_sym] = v } + if (update_params.keys - allowed_params).empty? + if update_params.include? :show + update_params[:show].map!{ |k, v| k.to_sym } + end + + params[:params].each do |k, v| + user[k] = v + end user.save end + @res[:user] = { :id => user.id, :name => user.name,