我有一个应用程序,它发送一封邀请电子邮件,其中包含要注册的链接但是寄存器链接给出了一个错误如果注册链接不存在,则会发送电子邮件。
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 => '[email protected]')
end
end
invitation.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
编辑:Rake管线
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
最佳答案
您可能在config/routes.rb中缺少这个
resources :invitations do
get 'accept_referral', :on => :collection
end
它将生成一个GET target-like/invitations/accept_引用,该引用将接受邀请,因此您可以
accept_referral_invitations_url()
现在,要获取邀请代码,请执行以下操作:
accept_referral_invitations_url(:invite_token => @invitation.invite_token)
这将导致类似于/invitations/accept_referration的URL?invite_token=blahblahblah邀请
注意,我在集合中添加了get目标(而不是成员),因此不需要传递@invitation对象本身。而且,因为它是一个集合目标,所以您使用invitation s-url(复数)而不是invitation-url。
希望能有帮助!