ruby on rails -在Rails3邮件中为路由/ url路径添加变量



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

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

邮件生成一个相对链接。如何预预主机路径?(我已经在develop .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的更新,并很好地发挥与设计。控制器现在看起来像这样

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而不是new_user_registration_path。Url =绝对,路径=相对。

你可能需要向我们展示你的路线来帮助解决第二个问题。看起来您的参数被视为一种格式。也许您需要自定义映射?比如:

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

既然您已经设置了default_url_options,我希望您能够在邮件视图中调用url helper,而不是从控制器传递它。

相关内容

  • 没有找到相关文章

最新更新