【问题标题】:Rails3 invitations and mailer - how to add variables to routes/urls?Rails3 邀请和邮件 - 如何将变量添加到路由/url?
【发布时间】:2011-04-29 10:39:23
【问题描述】:

这应该很简单,但我一直找不到答案。谁能指出我正确的方向?

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

我已设置邮件程序以发送邀请网址。一切正常,除了一个小问题。

mailer生成的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

感谢您的任何想法!

【问题讨论】:

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


    【解决方案1】:

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

    new_user_registration_url(@invitation.token)
    

    而不是 new_user_registration_path。

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

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

    希望这会有所帮助!

    【讨论】:

    • 感谢您的建议。你当然是正确的 _url 与 _path - 我的简单编辑错误。第二个建议有效 - 谢谢!但我很好奇,为什么这不是一个很好的解决方案?感谢您帮助我更好地理解。
    • 太棒了!第二种解决方案不如 new_user_registration_path(@invitation.token) (或类似的)简洁。我以前不得不做类似的事情,但感觉不“正确”。也许这只是我。据我所知,这可能非常好!
    【解决方案2】:

    改变你的路线

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

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

    def to_param
      "#{token}"
    end
    

    那么你可以简单地使用

    new_user_registration_url(@invitation)
    

    【讨论】:

      猜你喜欢
      • 2021-03-03
      • 2019-07-26
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多