未定义的方法 - 从邮件控制器中的部分调用帮助程序方法时出现问题



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

我在网上搜索了同样的问题,但发现没有简洁的回应 - 我担心我正在做一些基本的事情 somwe哪里错了

错误:

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

结束

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

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

最新更新