向列表用户发送电子邮件(未定义的变量或方法'email')

时间:2014-08-27 05:50:04

标签: ruby-on-rails ruby ruby-on-rails-3 email

我目前正在尝试开发以下功能:用户可以列出商家信息。其他人可以点击这些列表,点击申请,填写表格并点击提交。当然,这个表格应该发送给列表用户。我坚持让最后一部分正确。我正在使用ActionMailer,这就是我得到的:

listing_controller.rb

def apply
      end

    def send_resume_email
        UserMailer.new_resume(@user).deliver
        redirect_to contact_path, notice: 'Message sent'
    end

apply.html.erb

<div class="top">
<div class="container-content">
    <div class="container">
        <%= form_tag(send_resume_email_path) do %>
            <div class="form-group">
                <%= label_tag 'name', 'Name' %>
                <%= text_field_tag 'name', nil, class: 'form-control', placeholder: 'Your Name' %>
            </div>
           <div class="form-group">
               <%= label_tag 'email', 'Email' %>
               <%= email_field_tag 'email', nil, class: 'form-control', placeholder: 'Your Email Address' %>
           </div>
           <div class="form-group">
               <%= label_tag 'comments', 'Comments' %>
               <%= text_area_tag 'comments', nil, class: 'form-control', rows: 4, placeholder: 'Comments...' %>
           </div>
           <%= submit_tag nil, class: 'btn btn-default btn-about pull-right' %>
       <% end %>
  </div>
</div>
</div>

的routes.rb

 match '/send_resume_email', to: 'listings#send_resume_email', via: 'post'

user_mailer.rb

def new_resume(user)
    @user = user
    @url = 'http://www.example.com'
    mail(to: user.email, subject: 'Thanks for the awesome site')
end
end 

new_resume.html.rb

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>You've got a new resume, <%= @user.name %></h1>
    <p>
      You've got a new resume.<br>
    </p>
    <p>
      To login to the site, just follow this link: <%= @url %>.
    </p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

目前我收到此错误 未定义的方法`email&#39;为零:NilClass

提取的来源(第14行):

@user = user
@url = 'http://www.example.com'
mail(to: user.email, subject: 'Thanks for the awesome site')

非常感谢

1 个答案:

答案 0 :(得分:0)

您没有在控制器中定义@user,所以它没有。

所以你将nil传递给邮件程序,而不是用户记录。

您的邮件程序正在尝试邮寄到user.email nil.email,这会给您发错误'未定义的方法电子邮件为nil:NilClass'。