在过去的几天里,我正在尝试应用一个 beta 邀请系统。我遵循了 railcasts 第 124 课。我为我的用户定制了它。
我的路线.rb
resources :invitations, only: [:new, :create]
match '/signup/:invitation_token', to: 'users#new'
邀请模型:
class Invitation < ActiveRecord::Base
attr_accessible :new, :recipient_email, :user_id, :sent_at, :token
belongs_to :user
has_one :recipient, :class_name => 'User'
validates_presence_of :recipient_email
validate :recipient_is_not_registered
validate :user_has_invitations, :if => :user
before_create :generate_token
before_create :decrement_user_count, :if => :user
private
def recipient_is_not_registered
errors.add :recipient_email, 'zaten siteye üye' if User.find_by_email(recipient_email)
end
def user_has_invitations
unless user.invitation_limit > 0
errors.add_to_base 'Siteye davetiyeniz kalmamıştır.'
end
end
def generate_token
self.token = Digest::SHA1.hexdigest([Time.now, rand].join)
end
def decrement_user_count
user.decrement! :invitation_limit
end
end
邀请控制器:
class InvitationsController < ApplicationController
def new
@invitation = Invitation.new
end
def create
@invitation = Invitation.new(params[:invitation])
@invitation.user = current_user
if @invitation.save
if signed_in?
Mailer.deliver_invitation(@invitation, signup_path(@invitation.token))
flash[:notice] = "Teşekkürler,davetiniz gönderildi."
redirect_to root_path
else
flash[:notice] = "Teşekkürler,sizi almaya hazır olduğumuzda bildireceğiz."
redirect_to root_path
end
else
render :action => 'new'
end
end
end
. 这是我的 mailer.rb:
def invitation(invitation, signup_url)
subject 'Siteye Davet'
recipients invitation.recipient_email
from 'foo@example.com'
body :invitation => invitation, :signup_url => signup_path
invitation.update_attribute(:sent_at, Time.now)
end
当我发送邀请时,我的日志文件显示:
INSERT INTO "invitations" ("created_at", "recipient_email", "sent_at", "token", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Wed, 04 Jul 2012 21:55:13 UTC +00:00], ["recipient_email", "neveryt@gmail.com"], ["sent_at", nil], ["token", "c29e2dcc22c033a1e975f5755795db9f2a8fd5c2"], ["updated_at", Wed, 04 Jul 2012 21:55:13 UTC +00:00], ["user_id", 1]]
(131.3ms) commit transaction
Completed 500 Internal Server Error in 208ms
NoMethodError (undefined method `signup_path' for #<InvitationsController:0x000000044ef508>):
我可以理解我必须更改 signup_url 的。但我找不到正确的方法。如何修复邀请创建操作?