Rails3 invitations and mailer - how to add variabl

2019-05-30 03:47发布

问题:

This should be simple fix, but I've been unable to find an answer. Can anyone point me in the right direction?

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

I've set up the mailer to send out the invite urls. Everything works fine, apart from one small issue.

The url generated by the mailer is /user/sign_up.token.

I need to generate /user/sign_up/token (slash instead of period).

I guess I need to change the syntax in "Mailer.invitation().deliver", but I can't find any documentation to help. Can anyone point me in the right direction?

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

Invitations controller:

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

Mailer:

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

Thank you for any ideas!

回答1:

Not completely sure if this would work, but maybe try

new_user_registration_url(@invitation.token)

instead of new_user_registration_path.

Another (but not a very good) method would be

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

Hope this helps!



回答2:

Change your route to

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

and add this to your Invitation model:

def to_param
  "#{token}"
end

Then you can simply use

new_user_registration_url(@invitation)