2012-04-08 6 views
1

私はRyan BatesのRailscast#124:Beta Invitationsに取り組んでいます。私はすべてのコードを用意していますが、まだ2つの問題があります。Railsのメーラには、セッションの永続性とメールの送信に関する問題があります。

  1. セッションは電子メール送信プロセスでは維持されません。何らかの理由で、ユーザーの署名付き が友だちに招待状を送信すると、アプリはユーザーをサインアウトします。
  2. サインインしているユーザーはメールを送信できません。間違った引数の数についてのエラーメッセージ が表示されます。

私はこれらの2つの問題は関連しているとは思っていませんが、オフチャンスであると私は両方を言及するべきだと思いました。なぜ私はこれらの2つの問題に遭遇しているのですか?

ArgumentError in InvitationsController#create 

    wrong number of arguments (0 for 2) 
    Rails.root: /*********/MyApp 

    Application Trace | Framework Trace | Full Trace 
    app/mailers/user_mailer.rb:10:in `invitation' 
    app/controllers/invitations_controller.rb:16:in `create' 

コントローラ

class InvitationsController < ApplicationController 

    def new 
    @invitation = Invitation.new 
    end 

    def create 
    @invitation = Invitation.new(params[:invitation]) 
    @invitation.sender = current_user 
    if @invitation.save 
     if current_user?(nil) 
     flash[:notice] = "Thank you, we will notify when we are ready." 
     redirect_to root_path  
     else 
     UserMailer.invitation.(@invitation, signup_path(@invitation.token)).deliver 
     flash[:notice] = "Thank you, invitation sent." 
     redirect_to hunts_path 
     end 
    else 
     render :action => 'new' 
    end 
    end 

end 

メーラー/ User_Mailer.rb:メールを送信するときに

エラーメッセージは、ユーザーに署名したとして招待します。 10行目は「def invitation(invitation、signup_path)」と書かれています。

class UserMailer < ActionMailer::Base 
    default :from => "********" 

    def registration_confirmation(user) 
    @user = user 
    attachments["rails.png"] = File.read("#{Rails.root}/app/assets/images/rails.png") 
    mail(:to => "#{user.name} <#{user.email}>", :subject => "Registered") 
    end 

    def invitation(invitation, signup_path) 
    subject 'Invitation' 
    recipients invitation.recipient_email 
    body  :invitation => invitation, :signup_url => signup_path 
    invitation.update_attribute(:sent_at, Time.now) 
    end 


end 

モデル/ invitation.rb

class Invitation < ActiveRecord::Base 

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

    validates_presence_of :recipient_email 
    validate :recipient_is_not_registered 
    validate :sender_has_invitations, :if => :sender 

    before_create :generate_token 
    before_create :decrement_sender_count, :if => :sender 

    private 

    def recipient_is_not_registered 
    errors.add :recipient_email, 'is already registered' if User.find_by_email(recipient_email) 
    end 

    def sender_has_invitations 
    unless sender.invitation_limit > 0 
     errors.add_to_base 'You have reached your limit of invitations to send.' 
    end 
    end 

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

    def decrement_sender_count 
    sender.decrement! :invitation_limit 
    end 

end 

routes.rbを

resources :users, :invitations 
resources :sessions, :only => [:new, :create, :destroy] 

match '/signup/', :to => 'users#new' 
match '/signup/:invitation_token', :to => 'users#new' 
match '/signin', :to => 'sessions#new' 
match '/signout', :to => 'sessions#destroy' 
match '/contact', :to => 'pages#contact' 
match '/about', :to => 'pages#about' 
match '/help', :to => 'pages#help' 

root :to => "pages#home" 
match ':controller(/:action(/:id(.:format)))' 
+0

最初の問題を解決しましたか?それは現在、ナッツを動かしている。 –

+0

私は願っています。私は招待状からすべてのトークンを取り除いた。 –

+0

それはあまりにも悪いです。私は "@ invitation.sender = current_user"という行がログアウトを引き起こしていることに気付きましたが、なぜ私は理解できません。 –

答えて

2

さて、ので、これはセッションの永続性の問題を引き起こしている間には、ドットべきではありませんそれを見た減少!メソッドは、データベース内のremember_tokenも更新すると面白そうだと思っています。 Cookie のremember_tokenはもう有効ではありません。私はそこまでのActiveRecordで何を見つけるために別の質問を頼むよ

def decrement_sender_count 
    # sender.decrement! :invitation_limit 
    limit = sender.invitation_limit 
    sender.update_attributes(:invitation_limit => (limit.to_i-1))  
end 

:私が思いついた最初の醜い回避策はこれです。より良い答えhereを参照してください。

+0

問題の原因を嗅ぐことにおめでとう!よくできました。 –

2

あなたがまっすぐにあなたのコードからこれをコピーした場合、あなたの例外の原因となっている余分なドットを持っていますこの行の:

UserMailer.invitation.(@invitation, signup_path(@invitation.token)).deliver 

私は、ログを確認した後invitation.rbで

招待モデル、関連部分

belongs_to :sender, :class_name => 'User' 
[...] 
before_create :decrement_sender_count, :if => :sender 
[...] 
def decrement_sender_count 
    sender.decrement! :invitation_limit 
end 

:招待状と(

+0

ありがとうございました! –

関連する問題