0

過去数日間、私はベータ招待システムを適用しようとしています。レールキャストのレッスン124に従いました。そして、ユーザー向けにカスタマイズしました。

私のroutes.rb

resources :invitations, only: [:new, :create]
match '/signup/:invitation_token',  to: 'users#new'

招待モデル:

class Invitation < ActiveRecord::Base
  attr_accessible :new, :recipient_email, :user_id, :sent_at, :token

  belongs_to :user
    has_one :recipient, :class_name => 'User'

    validates_presence_of :recipient_email
    validate :recipient_is_not_registered
    validate :user_has_invitations, :if => :user

    before_create :generate_token
    before_create :decrement_user_count, :if => :user

    private

    def recipient_is_not_registered
        errors.add :recipient_email, 'zaten siteye üye' if User.find_by_email(recipient_email)
    end

    def user_has_invitations
        unless user.invitation_limit > 0
            errors.add_to_base 'Siteye davetiyeniz kalmamıştır.'
        end
    end

    def generate_token
        self.token = Digest::SHA1.hexdigest([Time.now, rand].join)
    end

    def decrement_user_count
        user.decrement! :invitation_limit
    end
end

Invitations_controller:

class InvitationsController < ApplicationController
    def new
        @invitation = Invitation.new
    end

    def create
        @invitation = Invitation.new(params[:invitation])
        @invitation.user = current_user
        if @invitation.save
            if signed_in?
                Mailer.deliver_invitation(@invitation, signup_path(@invitation.token))
                flash[:notice] = "Teşekkürler,davetiniz gönderildi."
                redirect_to root_path
            else
                flash[:notice] = "Teşekkürler,sizi almaya hazır olduğumuzda bildireceğiz."
                redirect_to root_path
            end
        else
    render :action => 'new'
        end
    end
end

。これは私のmailer.rbです:

def invitation(invitation, signup_url)
  subject    'Siteye Davet'
  recipients invitation.recipient_email
  from       'foo@example.com'
  body       :invitation => invitation, :signup_url => signup_path
  invitation.update_attribute(:sent_at, Time.now)
end

招待状を送信すると、ログファイルに次のように表示されます。

 INSERT INTO "invitations" ("created_at", "recipient_email", "sent_at", "token", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?)  [["created_at", Wed, 04 Jul 2012 21:55:13 UTC +00:00], ["recipient_email", "neveryt@gmail.com"], ["sent_at", nil], ["token", "c29e2dcc22c033a1e975f5755795db9f2a8fd5c2"], ["updated_at", Wed, 04 Jul 2012 21:55:13 UTC +00:00], ["user_id", 1]]
   (131.3ms)  commit transaction
Completed 500 Internal Server Error in 208ms

NoMethodError (undefined method `signup_path' for #<InvitationsController:0x000000044ef508>):

signup_urlを変更する必要があることは理解できます。しかし、正しい方法が見つかりません。招待状の作成アクションを修正するにはどうすればよいですか?

4

1 に答える 1

3

matchのステートメントを次のように変更する必要がありますroutes.rb

match '/signup/:invitation_token',  to: 'users#new', as: 'signup'

signup_pathこれにより、にリンクしている一致したルートの名前がRailsに通知され/signup/:invitation_tokenます。

于 2012-07-04T22:26:48.823 に答える