ruby on rails - Rails3邀请和邮件-如何向路由/url添加变量



这应该是简单的修复,但我一直无法找到答案。有人能给我指个方向吗?

我正在实现一个Rails3测试版邀请系统la Ryan Bates - http://railscasts.com/episodes/124-beta-invitations

我已经设置了邮件发送邀请的url。一切都很好,除了一个小问题。

邮件生成的url是/user/sign_up.token。

我需要生成/user/sign_up/token(斜杠而不是句号)。

我想我需要改变语法"Mailer.invitation().deliver",但我找不到任何文档来帮助。有人能给我指个方向吗?

我的路由文件的相关部分:

devise_for :users,  :path_prefix => 'registration', :controllers => {:registrations => 'users/registrations'} do
    get   "registration/users/sign_up/:invitation_token" => "users/registrations#new"
end

邀请控制器:

class InvitationsController < ApplicationController
  def new
    @invitation = Invitation.new
    @title = "Invite a friend"
  end
  def create
    @invitation = Invitation.new(params[:invitation])
    @invitation.sender = current_user
    if @invitation.save
        if user_signed_in?
            Mailer.invitation(@invitation, new_user_registration_path(@invitation.token)).deliver
            redirect_to root_url, :notice => "Thank you, your friend will receive their invitation soon."
        else
            redirect_to root_url, :notice => "Thank you, we'll let you know when the next batch of invites are availale."
        end
    else
        if current_user.invitation_limit > 0
            render :action => 'new', :alert => "Sorry, there was a problem! Please try a again."
        else
            redirect_to root_url, :alert => "Sorry, you don't have any invitations left. Please wait until we issue more."
        end
    end
  end
end

梅勒:

class Mailer < ActionMailer::Base
  def invitation(invitation, sign_up)
    subject     'Invitation'
    recipients  invitation.recipient_email
    @greeting = "Hi"
    @invitation = invitation
    @signup_url = sign_up
    @sender = invitation.sender_id
    invitation.update_attribute(:send_at, Time.now)       
  end
end

谢谢你的建议!

不完全确定这是否有效,但也许可以尝试

new_user_registration_url(@invitation.token)

代替new_user_registration_path。

另一个(但不是很好的)方法是

new_user_registration_url+"/#{@invitation.token}" #substitute path for url maybe

希望这对你有帮助!

将路由更改为

get   "registration/users/sign_up/:id" => "users/registrations#new"

并将其添加到您的邀请模型:

def to_param
  "#{token}"
end

那么你可以直接使用

new_user_registration_url(@invitation)

相关内容

  • 没有找到相关文章

最新更新