【发布时间】:2011-08-02 06:46:14
【问题描述】:
我们正在开发一种以这种方式工作的公告机制: - 用户是公告下的嵌套属性。用户的电子邮件作为嵌套属性输入到新的公告表单中。 - 希望以这种方式工作 - 保存公告时,它需要搜索用户的电子邮件和当前用户的电子邮件,如果找到,然后将此公告存入该用户的个人资料中,用户将收到电子邮件通知。如果不是,则创建一个新的用户帐户并发送一封电子邮件让用户认领他的帐户。 创建新公告时,rails 会保存所有与嵌套属性相关的属性(这很好,除非用户的电子邮件已经存在)。 由于仅使用用户的电子邮件来创建用户的帐户,我们以后如何实现密码(或自动创建密码以允许用户稍后更改)?我们正在使用设备进行身份验证。有没有办法使用设计来执行这个功能? 如何解决这个问题?非常感谢您的帮助。
class Announcement < ActiveRecord::Base
attr_accessible :content, :users_attributes
has_many :users, :through => :awards
has_many :awards, :dependent => :destroy
accepts_nested_attributes_for :users, :reject_if => lambda { |a| a[:email].blank? }, :allow_destroy => true
end
class User < ActiveRecord::Base
attr_accessible :email, :first_name, :last_name, :middle_name
has_many :awards, :dependent => :destroy
has_many :announcements, :through => :awards, :dependent => :destroy
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, :presence => true, :format => { :with => email_regex }, :uniqueness => { :case_sensitive => false }
end
电子邮件
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 web-applications