1

我有一个应用程序发送包含注册链接的邀请电子邮件。但是注册链接给出了错误。如果注册链接不存在,电子邮件将发送。

NoMethodError in Invitations#create
undefined method `accept_referral_invitation_url' for #(#(Class:0xadd8c90):0xb5d868cc)

错误发生在这一行

= accept_referral_invitation_url(@invitation.invite_token)

邀请控制器

class InvitationsController < ApplicationController
    def new
        @invitation = Invitation.new
    end

  def create
  @invitation = current_user.invitations.new(params[:invitation])

    if @invitation.valid?
      @invitation.save
      redirect_to root_url, :notice => "Invitation sent!"
      UserMailer.invitation(@invitation).deliver
    else
      render "new", :notice => "Somehting went wrong!"
    end
end


  def accept_referral
      @invitation = Invitation.find_by_invite_token(params[:invite_token])
      @invitation.accepted_at = Time.zone.now
      @invitation.save!
      redirect_to root_url, :notice => "Success!"
  end

end

用户邮件

class UserMailer < ActionMailer::Base
  def invitation(invitation)
    @invitation = invitation
    mail(:to => invitation.email, :subject => "You're invited", :from => 'alain@lootfly.com')
  end
end

邀请.html.haml(被发送的消息)

You have been cordially invited to join beta.
Click this link to sign up!
= accept_referral_invitation_url(@invitation.invite_token)
= accept_referral_invitation_url(invite_token: @invitation.invite_token)
# I TRIED BOTH OF THOSE LINKS^^^^ TO NO AVAIL!!!!!!!

邀请函模型

  create_table "invitations", :force => true do |t|
    t.integer  "user_id"
    t.text     "email"
    t.text     "invite_token"
  end

编辑:耙路线

        invitations GET    /invitations(.:format)                invitations#index
                    POST   /invitations(.:format)                invitations#create
     new_invitation GET    /invitations/new(.:format)            invitations#new
    edit_invitation GET    /invitations/:id/edit(.:format)       invitations#edit
         invitation GET    /invitations/:id(.:format)            invitations#show
                    PUT    /invitations/:id(.:format)            invitations#update
                    DELETE /invitations/:id(.:format)            invitations#destroy
4

2 回答 2

2

您的错误中的Invitations对象是可疑的,因为 Rails 通常不会创建该名称的类(即没有限定符的复数)。您应该检查您的Invitation类定义是否有不当使用该对象名称。

于 2013-06-25T17:21:14.770 回答
2

你可能在 config/routes.rb 中遗漏了这个

resources :invitations do
  get 'accept_referral', :on => :collection 
end

这将生成一个像 /invitations/accept_referral 这样接受邀请的 GET 目标,所以你可以这样做

accept_referral_invitations_url()

现在,要获取邀请码,请执行以下操作:

accept_referral_invitations_url(:invite_token => @invitation.invite_token)

这将导致像 /invitations/accept_referral?invite_token=blahblahblah 这样的 URL

请注意,我在集合(而不是成员)上添加了 GET 目标,因此我不需要传递 @invitation 对象本身。而且,因为它是一个收集目标,所以您使用invitation_url(复数)而不是invitation_url。

希望有帮助!

于 2013-06-25T17:16:54.860 回答