0

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

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

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

4

1 回答 1

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
于 2012-07-01T17:52:51.753 回答