开发者

Problem with url_for and named routes in ActionMailer View: "Need controller and action"

开发者 https://www.devze.com 2022-12-28 05:30 出处:网络
I\'m attempting to provide a confirmation link in my user welcome email and I\'m getting the following Rails error:

I'm attempting to provide a confirmation link in my user welcome email and I'm getting the following Rails error:

Need controller and action!

It makes a fuss about this line:

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

In my development.rb environment, I have the following line:

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

There's no problem with the @user variable. I've tested the email with things like @user.username and @user.confirmation_code. I'm only getting trouble with url_for and named routes like confirm_user_url.

When I check my routes with rake routes, confirm_user shows up, so it's not an issue with the named route not existing.

I can't seem to figure it out. What gives?


ActionMailer isn't a real Rails controller, so routes aren't available in your mailer templates. You need to set up instance variables in your mailer class to pass values to the template.

eg. if your mailer is 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

Then in the template:

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

This is explained somewhat cryptically in the api in the "ActionMailer" section, and in more detail in Agile Web Development with Rails, 3rd Ed., Chapter 25.


Since having a clickable url is what you said in your where after in your comment, here you go macek

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

I've used the auto_link helper in some of my mailers to make urls clickable and it has worked out pretty well. I think that does email addresses also, check out the full api for all the options. Enjoy.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号