3

ユーザーのウェルカム メールに確認リンクを提供しようとすると、次の Rails エラーが発生します。

Need controller and action!

この行について大騒ぎします:

<p>Please take a moment to activate your account by going to: 
<%= link_to confirm_user_url(:id => @user.confirmation_code) %>.</p>

私のdevelopment.rb環境では、次の行があります。

config.action_mailer.default_url_options = {
  :host => "localhost", :port => 3000
}

@user変数に問題はありません。@user.usernameや などでメールをテストしました@user.confirmation_codeurl_forのような名前付きルートでのみ問題が発生しconfirm_user_urlます。

でルートを確認するとrake routesconfirm_userが表示されるため、名前付きルートが存在しないという問題はありません。

私はそれを理解できないようです。何を与える?

4

2 に答える 2

10

ActionMailer は実際の Rails コントローラーではないため、メーラー テンプレートでルートを使用することはできません。テンプレートに値を渡すには、メーラー クラスでインスタンス変数を設定する必要があります。

例えば。メーラーが SampleMailer の場合:

class SampleMailer < ActionMailer::Base

  def confirmation_mail(user)
    subject    'Confirmation'
    recipients user.email
    from       'sample@example.com'
    sent_on    Time.now
    body       :greeting => 'Sample Greeting', :email => user.email,
               :confirm_user_url => confirm_user_url(:id=>@user.confirmation_code)
  end
end

次に、テンプレートで:

<%= link_to "Confirmation link", @confirm_user_url %>

これは、「ActionMailer」セクションのapiでやや不可解に説明されており、Rails を使用したアジャイル Web 開発、第 3 版でより詳細に説明されています。、第25章。

于 2010-04-21T23:47:01.643 に答える
3

クリック可能なURLを持つことは、コメントのどこであなたが言ったことなので、ここであなたはマセックに行きます

<p>Please take a moment to activate your account by going to: 
<%= auto_link confirm_user_url(:id => @user.confirmation_code) %>.</p>

一部のメーラーでヘルパーを使用auto_linkしてURLをクリック可能にしましたが、かなりうまくいきました。メールアドレスもそうだと思います。すべてのオプションについては、完全なAPIを確認してください。楽しみ。

于 2010-04-23T04:51:27.717 に答える