undefined方法 - 从邮件控制器中的部分调用辅助方法的问题

时间:2017-11-01 10:37:51

标签: ruby-on-rails

我已经设置了一个ActionMailer来发送电子邮件并拉出一部分来为用户发布数据,但是帮助方法正在以未定义的方式返回 - 我将它们移动到应用程序助手但仍然是同样的错误,我认为它在我将变量传递给邮件的方式?

我在网上搜索过同样的问题,但发现没有简洁的回复 - 我担心我做的事情基本上是错误的

错误:

undefined method `tidy_address' for #<#<Class:0x007f5c90681b10>:0x007f5c90ad8ba0>

我的部分订单视图:_enquiry_details.html.erb

<div class="row">  
 <div class="col-xs-2">
    <h3><%= @customer.name %></h3>
    <hr>
    <h5><%=  tidy_address(@customer.locations.first) %></h5>
    <% @phone_number.each do |pn| %>
    <h5><%= pn.name %> : <%=pn.phone_number.phone%></h5>
    <% end %>

在我的用户mailer.rb

  def lead_received(enquiry)
    @order=enquiry
     if @order.user
      @customer=@order.user
     else
      @customer=@order.company
     end
    @locations=@customer.locations
    @phone_number=@customer.phone_maps
    mail to: "myemailaddress@domain.com", subject: "New Lead Received"

   end

我打电话给这个通过命令,认为这是我出错的地方

按顺序控制..

if @order.save
    UserMailer.lead_received(@order).deliver_now

为了清晰起见,请参阅我的邮件浏览器lead_received.html.erb

<%= render "orders/enquiry_details" %>

最后在我的位置助手

module LocationsHelper

  def google_string(lat,long,size)
    case size
    when "s"
      mysize="150x150&zoom=12"
    when "m"
      mysize="350x300&zoom=14"
    when "l"
      mysize="570x300&zoom=13&scale=2"
  end
  "https://maps.googleapis.com/maps/api/staticmap?"+URI.encode("markers=#{lat},#{long}&size=#{mysize}&key=AIzaSyAxRuThoVl-xziFElt3GPCESLsaye4_aGA")  
  end

  # Return a sorted neat adress block
  def tidy_address(location)
    unless location.blank?
    t_address=""
    t_address="#{location.address1}<br>" if location.address1.present?
    t_address=t_address+location.address2+"<br>" if location.address2.present?
    t_address=t_address+location.address3+"<br>" if location.address3.present?
    t_address=t_address+location.city+"<br>" if location.city.present?
    t_address=t_address+location.postcode if location.postcode.present?
    # t_address=t_address+"("+location.id.to_s+")"
    #t_address=t_address+"<br><a href=''>Directions to here</a>"
    t_address.html_safe
  else
    t_address="<link_to 'Add an address' '#'>".html_safe
  end
end

1 个答案:

答案 0 :(得分:1)

在邮件程序代码中添加帮助程序以在邮件程序内使用。

class UserMailer < ActionMailer::Base

  default from: "" # default from email
  helper LocationsHelper
  helper UserHelper      

  def lead_received(enquiry)
    @order=enquiry

    if @order.user
      @customer=@order.user
    else
      @customer=@order.company
    end

    @locations=@customer.locations
    @phone_number=@customer.phone_maps
    mail to: "myemailaddress@domain.com", subject: "New Lead Received"
  end
end