我有一个应用程序,现在,我需要保存用户的偏好 我做了以下事情:
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
问题是:如何制作表格以保存用户的偏好?我根本不知道......
任何事情都赞赏:)
答案 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