使用Ryan Bate的RailsCasts#124 Beta Invites(以及更新的rails 3.1 api)作为拐杖,我正在努力整理我的第一部Action Mailer功能:邀请某人与您合作完成一个项目。
我的问题是:recipient_email
没有保存在数据库中,我看不到我错过的内容。
配置/初始化/为setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'blah.com',
:user_name => 'gmail username',
:password => 'gmail password',
:authentication => 'plain',
:enable_starttls_auto => true
}
ActionMailer::Base.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?
应用程序/模型/ invitation.rb
class Invitation < ActiveRecord::Base
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
attr_accessor :recipient_email
belongs_to :sender, :class_name => "User", :foreign_key => "sender_id"
has_one :recipient, :class_name => "User", :foreign_key => "recipient_id"
validates_presence_of :recipient_email, :on => :create, :message => "can't be blank"
validates :recipient_email, :format => email_regex
validate :recipient_is_not_registered
before_create :generate_token
def sender_name
sender.user_name
end
def sender_email
sender.email
end
private
def recipient_is_not_registered
exsisting_user = User.find_by_email(recipient_email)
if exsisting_user
errors.add :recipient_email, 'is already a member.'
else
recipient_email
end
end
def generate_token
self.token = Digest::SHA1::hexdigest([Time.now, rand].join)
end
end
app / models / user.rb(减去所有auth的东西)
class User < ActiveRecord::Base
attr_accessible :invitation_token
has_many :sent_invitations, :class_name => "Invitation", :foreign_key => "sender_id"
belongs_to :invitation
end
应用程序/控制器/ invitations_controller.rb
class InvitationsController < ApplicationController
before_filter :authenticate
def new
@title = "Invite client"
@invitation = current_user.sent_invitations.new
end
def create
@invitation = current_user.sent_invitations.create!(params[:invitation])
sender_name = @invitation.sender_name
sender_email = @invitation.sender_email
if @invitation.save
Mailer.invitation(@invitation, signup_url(@invitation.token), sender_name, sender_email).deliver
flash[:success] = "Your client has been sent the email. Why not create a booking for them?"
redirect_to bookings_path
else
@title = "Invite client"
render :new
end
end
end
应用程序/邮寄者/ mailer.rb
def invitation(invitation, signup_url, sender_name, sender_email)
@signup_url = signup_url
@sender_name = sender_name
@sender_email = sender_email
mail(:to => invitation.recipient_email,
:subject => "Invitation to Join",
:from => @sender_email)
end
应用程序/视图/邀请/ _invitation_form.html.erb
<%= form_for @invitation do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<%= f.hidden_field :email_token %>
<br />
<div class="field">
<%= f.label :recipient_email, "Client's email address" %>
<%= f.email_field :recipient_email %>
</div>
<br />
<div class="action">
<%= f.submit "Send invitation", :class => "a small white button radius" %>
</div>
<% end %>
显示:recipient_email未保存的SQL日志
Started POST "/invitations" for 127.0.0.1 at 2011-12-14 21:27:11 +1100
Processing by InvitationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7/SGZypGXtf9ShlcjC6o8ZRj2Qe4OJTHdjis2/m3ulc=", "invitation"=>{"recipient_email"=>"users@email.com"}, "commit"=>"Send invitation"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(0.1ms) BEGIN
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'users@email.com' LIMIT 1
SQL (0.4ms) INSERT INTO "invitations" ("created_at", "recipient_email", "sender_id", "sent_at", "token", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["created_at", Wed, 14 Dec 2011 10:27:11 UTC +00:00], ["recipient_email", nil], ["sender_id", 1], ["sent_at", nil], ["token", "56fba1647d40b53090dd49964bfdf060228ecb2d"], ["updated_at", Wed, 14 Dec 2011 10:27:11 UTC +00:00]]
(10.2ms) COMMIT
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(0.1ms) BEGIN
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'users@email.com' LIMIT 1
(0.1ms) COMMIT
Rendered mailer/invitation.text.erb (0.4ms)
Sent mail to users@email.com (7ms)
Date: Wed, 14 Dec 2011 21:27:11 +1100
From: admin@email.com
答案 0 :(得分:1)
可能是attr_accessor :recipient_email
模型中的Invitation
行。拿出那条线,因为recipient_email
是一个数据库字段,不是吗?