Rails3邀请函和邮件程序 - 如何向路由/网址添加变量?

时间:2011-04-29 10:39:24

标签: ruby-on-rails devise ruby-on-rails-3

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

我正在实施Rails3 beta邀请系统和Ryan Bates - http://railscasts.com/episodes/124-beta-invitations

我已设置邮件发送邀请网址。除了一个小问题外,一切都很好。

邮件程序生成的网址为/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

感谢您的任何想法!

2 个答案:

答案 0 :(得分:3)

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

new_user_registration_url(@invitation.token)

而不是new_user_registration_path。

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

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

希望这有帮助!

答案 1 :(得分:0)

将路线改为

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

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

def to_param
  "#{token}"
end

然后你可以简单地使用

new_user_registration_url(@invitation)