【问题标题】:undefined method - issues calling a helper method from a partial in a mail controller未定义的方法 - 从邮件控制器中的部分调用辅助方法的问题
【发布时间】:2017-11-01 10:37:51
【问题描述】:

我已经设置了一个 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

结束

【问题讨论】:

  • 检查你是否在私有块中定义了tidy_address
  • gravatar_for 不是应用程序助手的错误。你在哪里使用gravatar_for
  • 它是从用户控制器调用的,它也抛出了同样的错误,我把它注释掉以简化问题和测试
  • Anand - 更改为包含完整的帮助内容,因此不公开。
  • 可以在调用gravatar_for方法的地方添加代码吗?

标签: ruby-on-rails


【解决方案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

【讨论】:

    猜你喜欢
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2012-02-13
    • 2011-11-19
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    相关资源
    最近更新 更多