【问题标题】:Rails mailer has issues with session persistence and with sending emailRails 邮件程序在会话持久性和发送电子邮件方面存在问题
【发布时间】:2012-04-08 15:41:46
【问题描述】:

我正在研究 Ryan Bates 的 Railscast #124:Beta 邀请。我已经准备好所有代码,但我还有两个问题。

  1. 会话不会在电子邮件发送过程中持续存在。出于某种原因,当签署 在用户向朋友发送邀请时,应用会注销用户。
  2. 已登录的用户无法发送电子邮件。我收到一条错误消息 关于参数数量错误。

我不认为这两个问题是相关的,但如果它们是相关的,我想我应该同时提到这两个问题。有没有人知道为什么我会遇到这两个问题?

以登录用户身份发送电子邮件邀请时出现错误消息:

    ArgumentError in InvitationsController#create

    wrong number of arguments (0 for 2)
    Rails.root: /*********/MyApp

    Application Trace | Framework Trace | Full Trace
    app/mailers/user_mailer.rb:10:in `invitation'
    app/controllers/invitations_controller.rb:16:in `create'

控制器

class InvitationsController < ApplicationController

  def new
    @invitation = Invitation.new
  end

  def create
    @invitation = Invitation.new(params[:invitation])
    @invitation.sender = current_user
    if @invitation.save
      if current_user?(nil) 
         flash[:notice] = "Thank you, we will notify when we are ready."
         redirect_to root_path       
      else
        UserMailer.invitation.(@invitation, signup_path(@invitation.token)).deliver
        flash[:notice] = "Thank you, invitation sent."
        redirect_to hunts_path 
      end
    else
      render :action => 'new'
    end
  end

end

邮件程序/User_Mailer.rb。第 10 行是“definvitation(invitation, signup_path)”。

class UserMailer < ActionMailer::Base
  default :from => "********"

  def registration_confirmation(user)
    @user = user
    attachments["rails.png"] = File.read("#{Rails.root}/app/assets/images/rails.png")
    mail(:to => "#{user.name} <#{user.email}>", :subject => "Registered")
  end

  def invitation(invitation, signup_path)
    subject    'Invitation'
    recipients invitation.recipient_email
    body       :invitation => invitation, :signup_url => signup_path
    invitation.update_attribute(:sent_at, Time.now)
  end


end

models/invitation.rb

class Invitation < ActiveRecord::Base

  belongs_to :sender, :class_name => 'User'
  has_one :recipient, :class_name => 'User'

  validates_presence_of :recipient_email
  validate :recipient_is_not_registered
  validate :sender_has_invitations, :if => :sender

  before_create :generate_token
  before_create :decrement_sender_count, :if => :sender

  private

  def recipient_is_not_registered
    errors.add :recipient_email, 'is already registered' if User.find_by_email(recipient_email)
  end

  def sender_has_invitations
    unless sender.invitation_limit > 0
      errors.add_to_base 'You have reached your limit of invitations to send.'
    end
  end

  def generate_token
    self.token = Digest::SHA2.hexdigest([Time.now, rand].join)
  end

  def decrement_sender_count
    sender.decrement! :invitation_limit
  end

end

Routes.rb

resources :users, :invitations
resources :sessions, :only => [:new, :create, :destroy]

match '/signup/',  :to => 'users#new'
match '/signup/:invitation_token', :to => 'users#new'
match '/signin',  :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/contact', :to => 'pages#contact'
match '/about',   :to => 'pages#about' 
match '/help',    :to => 'pages#help'

root :to => "pages#home"
match ':controller(/:action(/:id(.:format)))'

【问题讨论】:

  • 你解决过第一个问题吗?它目前让我发疯。
  • 我希望。我最终从我的邀请中删除了所有令牌。
  • 那太糟糕了。我注意到“@invitation.sender = current_user”行导致注销,但我不知道为什么。

标签: ruby-on-rails rails-activerecord


【解决方案1】:

好的,所以这导致了会话持久性问题:

invitation Modelininvitation.rb,相关部分

belongs_to :sender, :class_name => 'User'
[...]
before_create :decrement_sender_count, :if => :sender
[...]
def decrement_sender_count
  sender.decrement! :invitation_limit
end

查看日志后,我发现 decrement! 方法认为更新数据库中的 remember_token 也很有趣。然后是 cookie 中的 remember_token 不再有效。我想出的第一个丑陋的解决方法是:

def decrement_sender_count
  # sender.decrement! :invitation_limit
  limit = sender.invitation_limit
  sender.update_attributes(:invitation_limit => (limit.to_i-1))      
end

我会问另一个问题以了解 ActiveRecord 的情况。查看更好的答案here。

【讨论】:

  • 恭喜您找到了问题的根源!干得好。
【解决方案2】:

如果您直接从代码中复制了此内容,那么您有一个额外的点会导致此行中的异常:

UserMailer.invitation.(@invitation, signup_path(@invitation.token)).deliver

邀请和 (

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    相关资源
    最近更新 更多