2012-07-01 68 views
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 

的问题是:如何才能让一个形式,以节省用户的喜好?我不知道在所有...
什么是赞赏:)

回答

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 
+0

我做了这样的形式:HTTPS://gist.github。 com/3029121,我看到'#<#:0xa213234>' –

+0

'未定义的方法'settings_path'你必须编辑你的路线。添加'recources:settings' –

+0

现在它说我需要'create'方法...我该如何保存? –