将变量添加到 Rails3 邮件程序中的路由/ url 路径

发布于 2024-11-03 06:19:22 字数 2079 浏览 0 评论 0原文

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

我正在实施 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

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

谢谢!

I've been searching for an answer to what should be a simple question. Can anyone point me in the right direction, or at least tell me what I should be searching for?

I'm implementing a Rails3 beta invite system a la Ryan Bates - http://railscasts.com/episodes/124-beta-invitations

The mailer generates a relative link. How to I preprend the host path? (I already have config.action_mailer.default_url_options set up in development.rb)

--
The relevant bits of my routes file.

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

I've made some minor adjustments to reflect updates in Rails and to play nicely with Devise. The controller now looks like this

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

And the mailer like this:

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

I appreciate any pointers that would help be better understand why this is happening.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风吹 2024-11-10 06:19:22

第一个问题是您需要 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 帮助程序,而不是从控制器传递它。

The first problem is that you need new_user_registration_url instead of new_user_registration_path. Url = absolute, path = relative.

You may need to show us your routes for help with the second problem. It looks like your parameter is being treated as a format. Perhaps you need a custom mapping? Something like:

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

Since you've set up default_url_options, I would expect you to be able to call the url helper in the mailer view, rather than passing it in from the controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文