邀请函的未定义方法`signup_path'(仍然无法发送测试邀请)

时间:2012-07-04 22:20:42

标签: ruby-on-rails actionmailer

在最后几天,我正在尝试应用测试版邀请系统。我跟踪了第124课的轨道广播。我为我的用户定制了它。

我的routes.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

Invitations_controller:

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

。 这是我的邮件: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。但我找不到正确的方法。我如何修复邀请创建动作?

1 个答案:

答案 0 :(得分:3)

您需要将match中的routes.rb声明更改为:

match '/signup/:invitation_token',  to: 'users#new', as: 'signup'

这将告知Rails signup_path是链接到/signup/:invitation_token的匹配路由的名称。