2012-06-22 65 views
2

我使用的設計和devise_invitable爲什麼設計

gem 'devise',   '>= 2.0.0' 
gem 'devise_invitable', '~> 1.0.0' 

invitable不發送一些電子郵件和我在網站的管理員部分的鏈接自動發送邀請電子郵件給用戶

= link_to 'Send Invitation', invite_user_path(@user), :remote => true, :title => "Sends an email directly to the user" 


def invite 
    @user = User.find(params[:id]) 
    User.invite!(:email => @user.email) 
    flash.now[:success] = "Invitation email has been sent" 
    respond_to do |format| 
    format.js 
    end 
end 

這將發送電子郵件給用戶誰已經在數據庫user.invitation_token不爲NULL,但跳過發送電子郵件給其他用戶....這是爲什麼,我該如何解決這個問題。同樣在devise_invitable的文檔中,這是他們說呼叫邀請的方式!

User.invite!(:email => "[email protected]", :name => "John Doe") 

但是當我做,他們說我不能大規模分配屬性名稱

任何幫助,將不勝感激

我注意到,如果在用戶invitation_token場什麼的電子郵件會了,但如何創建,最初

UPDATE ...這裏是我的整個用戶模型

class User < ActiveRecord::Base 
    delegate :can?, :cannot?, :to => :ability 

    has_many :roles, :through => :role_users 
    has_many :role_users 
    has_many :notifications, :through => :subscriptions 
    has_many :subscriptions 
    has_many :companies, :through => :positions 
    has_many :positions 
    has_many :notification_histories 
    has_many :sites, :through => :site_users 
    has_many :site_users 

    scope :admins, joins(:roles).where("roles.name = 'SuperAdmin' or roles.name = 'SubAdmin'") 
    scope :regular, joins(:companies).where('positions.regular_user = 1').group('users.id') 
    scope :employee, joins(:companies).where('positions.regular_user = 0').group('users.id') 
    scope :current, :conditions => { :active => true }, :order => 'LOWER(first_name), LOWER(last_name) ASC' 

    default_scope :order => 'LOWER(first_name) ASC' 

    has_many :feedbacks do 
    def for_playlist(id) 
     find_or_create_by_playlist_id(id) 
    end 
    end 

    def name 
    "#{self.first_name} #{self.last_name}" 
    end 

    has_many :ratings, :through => :feedbacks do 
    def for_playlist(id) 
     where('feedbacks.playlist_id = ?', id) 
    end 
    end 

    Role::TYPES.each do |role| 
    define_method(role + '?') do 
     self.has_role?(role) 
    end 
    end 

    def has_role?(role_name) 
    role_name = role_name.name if role_name.is_a?(Role) 
    self.roles.where(:name => role_name.to_s).exists? 
    end 

    accepts_nested_attributes_for :roles, :notifications, :allow_destroy => true 

    def company 
    self.companies.first 
    end 

    def site 
    self.sites.first 
    end 

    validates :first_name, :presence => true 
    validates :last_name, :presence => true 
    validates :email, :presence => true 
    validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create, :if => :email_present? 
    validates_uniqueness_of :email 
    validates_format_of :phone_number, :with => /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/, :allow_nil => true 
    validates_format_of :password, 
         :with => /^.*(?=.{6,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W]).*$/, 
         :message => "must be at least 6 characters, have one number and one capital letter", 
         :if => Proc.new { |user| !user.password.blank? } 

    before_validation :clear_empty_attrs 
    before_validation :clean_data 

    # Include default devise modules 
    devise :database_authenticatable, :recoverable, :rememberable, :trackable, :invitable 

    # Setup accessible (or protected) attributes for devise support 
    attr_accessible :email, :password, :password_confirmation, :remember_me, :active, 
    :company_id, :first_name, :last_name, :phone_number, :role_ids, :notification_ids, :name 

    def role?(role) 
    return !!self.roles.find_by_name(role.to_s.camelize) 
    end 

    def ability 
    @ability ||= Ability.new(self) 
    end 

    def name 
    "#{first_name} #{last_name}" 
    end 

    def list_companies 
    companies.map(&:name).try(:join, ", ").try(:titlecase) 
    end 

    def email_present? 
    !self.email.blank? 
    end 

protected 
    def clear_empty_attrs 
    @attributes.each do |key,value| 
     self[key] = nil if value.blank? 
    end 
    end 

    def clean_data 
    self.email.downcase! unless self.email.nil? 
    end 
end 
+0

是:attr_accessible在您的用戶模式:名下的上市? 'attr_accessible:name,:email' –

+0

更好的問題是,您可以輸出您的用戶模型嗎? –

+0

確定一秒...... – Trace

回答

1

的色器件,invitable文檔指出需要

devise :database_authenticatable, :confirmable, :invitable 

爲它工作。您已在某處放棄了:confirmable,這是devise的一部分,可爲尚未擁有該令牌的用戶自動生成該令牌。在模型中這行應該是:這取決於你想要完整的電子郵件功能,工作什麼樣的環境

devise :database_authenticatable, :recoverable, :rememberable, :trackable, :confirmable, :invitable 
+0

只要:name字段去了,如果你希望能夠引用User's:name字段,那麼你應該添加一個'def name ='setter方法來補充你的' def name' getter方法。看看'attr_accessor'。 –

+0

讓我現在測試...感謝幫助我這個 – Trace

+0

沒有問題!您可能需要再次運行安裝腳本,不確定。 –

3

config/environments,設立的ActionMailer CONFIGS是一個先決條件。例如,如果你想在發展中發送電子郵件,這應該是目前在development.rbproduction.rb生產):

# change to true to allow email to be sent during development 
    config.action_mailer.perform_deliveries = false 

    config.action_mailer.smtp_settings = { 
    address: "smtp.gmail.com", 
    port: 587, 
    domain: "example.com", 
    authentication: "plain", 
    enable_starttls_auto: true, 
    user_name: ENV["GMAIL_USERNAME"], # you can use ordinary gmail username here 
    password: ENV["GMAIL_PASSWORD"] # you can use your gmail password here, but don't push the changes 
    } 
相關問題