如何保存用户首选项

时间:2012-07-01 17:39:15

标签: ruby-on-rails ruby-on-rails-3 mongodb devise mongoid

我有一个应用程序,现在,我需要保存用户的偏好 我做了以下事情:

class User
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_one :setting
end

class Setting
  include Mongoid::Document
  include Mongoid::Timestamps

  belongs_to :user

  field :notify, type: Boolean
end

问题是:如何制作表格以保存用户的偏好?我根本不知道......
任何事情都赞赏:)

1 个答案:

答案 0 :(得分:1)

form_for current_user.settings || current_user.build_settings

同时创建控制器

class SettingsController < ApplicationController
  def create
    settings = Settings.new params[:settings]
    if settings.save
      flash[:notice] = 'Settings saved'
    else
      flash[:error] = 'Settings could not be saved'
    end
    redirect_to :back
  end

  def update
    settings = Settings.find params[:id]
    if settings.update_attributes params[:settings]
      flash[:notice] = 'Settings saved'
    else
      flash[:error] = 'Settings could not be saved'
    end
    redirect_to :back
  end
end