在Rails3邮件程序中添加变量到routes / url路径

时间:2011-04-27 00:55:12

标签: ruby-on-rails routes ruby-on-rails-3 mailer

我一直在寻找一个简单问题的答案。任何人都可以指出我正确的方向,或者至少告诉我我应该寻找什么?

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

邮件程序生成相对链接。如何预先建立主机路径? (我已经在development.rb中设置了config.action_mailer.default_url_options)

- 我的路线文件的相关位。

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

我做了一些小的调整,以反映Rails中的更新,并与Devise很好地配合。控制器现在看起来像这样

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

我感谢任何有助于更好地理解为什么会发生这种情况的指示。

谢谢!

1 个答案:

答案 0 :(得分:1)

第一个问题是您需要new_user_registration_url而不是new_user_registration_path。 Url =绝对,路径=相对。

您可能需要向我们展示您的第二个问题的帮助路线。看起来您的参数被视为格式。也许您需要自定义映射?类似的东西:

match '/users/sign_up/:token' => 'users#sign_up', :as => :new_user_registration

由于您已设置default_url_options,我希望您能够在邮件程序视图中调用url帮助程序,而不是从控制器中传入它。