복붙노트

[RUBY-ON-RAILS] 어떻게 내 ActionMailer보기에 내보기 도우미를 사용 하는가?

RUBY-ON-RAILS

어떻게 내 ActionMailer보기에 내보기 도우미를 사용 하는가?

내 ReportMailer보기에 I 앱 / 헬퍼 / annotations_helper.rb에 정의 된 방법을 사용하려면 (응용 프로그램 / 뷰 / report_mailer / usage_report.text.html.erb). 이걸 어떻게해야합니까?

이 가이드를 바탕으로 내가 원하는 것을 할 수있는 add_template_helper (helper_module) 방법처럼 보인다,하지만 난 그것을 사용하는 방법을 알아낼 수 없습니다.

(BTW, 당신은 우편물 뷰에서 헬퍼의 다른 세트에 대한 액세스 권한을 얻을 이유가?이 꽤 성가신입니다.)

해결법

  1. ==============================

    1.당신이 당신의 이메일을 관리하기 위해 사용하고있는 메일러 클래스 :

    당신이 당신의 이메일을 관리하기 위해 사용하고있는 메일러 클래스 :

    class ReportMailer < ActionMailer::Base
      add_template_helper(AnnotationsHelper)
    
      ...
    end
    
  2. ==============================

    2.레일 3에서, 당신의 ActionMailer 클래스의 상단에있는 도우미 메서드를 사용합니다 :

    레일 3에서, 당신의 ActionMailer 클래스의 상단에있는 도우미 메서드를 사용합니다 :

    helper :mail   # loads app/helpers/mail_helper.rb & includes MailHelper
    

    나는 단지 하나의 우편에 필요하기 때문에 난 그냥, 블록 전달 :

    helper do
      def host_url_for(url_path)
        root_url.chop + url_path
      end
    end
    

    (설정 config.action_mailer.default_url_options해야합니다.)

    (: only_path => 거짓이 url_for를 사용하는 경우에 통과해야합니다)

  3. ==============================

    3.레일 3의 모든 우편물의 경우 ( "응용 프로그램"도우미 설정) :

    레일 3의 모든 우편물의 경우 ( "응용 프로그램"도우미 설정) :

    # config/application.rb:
    ...
    config.to_prepare do
      ActionMailer::Base.helper "application"
    end
    
  4. ==============================

    4.레일 4 루비, 나는이 일을했다 :

    레일 4 루비, 나는이 일을했다 :

    (1)으로 공작은 이미 추가 할 도우미 UsersHelper 예를 들어, 다음 추가 할 경우 말했다

    helper :users
    

    파생 ActionMailer 클래스 (예를 들어, 애플리케이션 / 메일러 / user_mailer.rb)

    (2) 그 후, 나는 새로운 오류가 발생했습니다 :

    ActionView::Template::Error (Missing host to link to! Please provide the :host
    parameter, set default_url_options[:host], or set :only_path to true)
    

    이 문제를 해결하려면 줄을 추가

    config.action_mailer.default_url_options = { :host => 'localhost' }
    

    는 config / 환경 / *. RB 파일의 각. 설정 / 환경 / production.rb를 들어, 생산 도우미 - 생성 된 URL에 대한보다 적절한 호스트와 로컬 호스트 교체합니다.

    Q : # 2의 경우, 왜 메일보기 필요는이 정보를 수행하고, 일반보기는하지?

    A : 일반 뷰가 생성 된 모든 링크가 링크 호스트에서 제공되기 때문에, 호스트를 알 필요가 없습니다 때문입니다. (당신은 hotmail.com 또는 gmail.com 등으로 연결되지 않는) 이메일에 표시 링크가 동일한 호스트에서 제공되지 않습니다

  5. ==============================

    5.당신은 당신의 우편물에 추가 할 수 있습니다

    당신은 당신의 우편물에 추가 할 수 있습니다

    helper :application
    

    또는 당신이 필요로하는 도우미 어떤

  6. ==============================

    6.(이것은 오래된 질문이다 그러나 나는 레일 5.2에서 나를 위해 작동하는 무슨 공유를 해요 그래서 레일이 진화하고있다.)

    (이것은 오래된 질문이다 그러나 나는 레일 5.2에서 나를 위해 작동하는 무슨 공유를 해요 그래서 레일이 진화하고있다.)

    일반적으로 당신은 이메일의 제목 줄뿐만 아니라 HTML 렌더링에 사용자 지정보기 도우미를 사용할 수도 있습니다. 다음과 같이보기 도우미 앱 / 헬퍼 / application_helper.rb에있는 경우 :

    module ApplicationHelper
    
      def mydate(time, timezone)
        time.in_time_zone(timezone).strftime("%A %-d %B %Y")
      end
    
    end
    

    나는 동적 이메일 제목 줄과 템플릿 모두 사용 헬퍼를 만들 수 있지만, 두 번째 여기에 세 번째 라인에서 볼 수 있듯이 나는, 명시 적으로 응용 프로그램 / 우편물 / user_mailer.rb 두 가지 방법으로 ApplicationHelper를 사용하여 레일을 말할 필요가 :

    class UserMailer < ApplicationMailer
    
      include ApplicationHelper  # This enables me to use mydate in the subject line
      helper :application  # This enables me to use mydate in the email template (party_thanks.html.erb)
    
      def party_thanks
        @party = params[:party]
        mail(to: 'user@domain.com',
        subject: "Thanks for coming on #{mydate(@party.created_at, @party.timezone)}")
      end
    
    end
    

    나는이 두 줄이 그렇게 둘 중 하나를 선택 단지뿐만 아니라 작동하는지 언급해야한다 :

    helper :application
    
    add_template_helper(ApplicationHelper)
    

    FWIW,이 같은 응용 프로그램 / 뷰 / user_mailer / party_thanks.html.erb의 외모에 이메일 템플릿 :

    <p>
      Thanks for coming on <%= mydate(@party.created_at, @party.timezone) %>
    </p>
    

    그리고이 같은 응용 프로그램 / 컨트롤러 / party_controller.rb 컨트롤러 외모

    class PartyController < ApplicationController
      ...
      def create
        ...
        UserMailer.with(party: @party).party_thanks.deliver_later
        ...
      end
    end
    

    나는 OP (@ 톰 레먼)이 모든 Feel로는 꽤 아마도 내가 뭔가를 놓친 거지 https://guides.rubyonrails.org/action_mailer_basics.html#using-action-mailer-helpers 주어진 복잡한 것을 @gabeodess에 동의해야합니다 .. .

  7. ==============================

    7.Rails4에 대한 필자의 경우는 다음과 같이 수행

    Rails4에 대한 필자의 경우는 다음과 같이 수행

    # app/mailers/application_mailer.rb
    class ApplicationMailer < ActionMailer::Base
      add_template_helper ApplicationHelper
      ...
    end
    

    # app/mailers/user_mailer.rb
    class AccountMailer < ApplicationMailer
      def some_method(x, y)
      end
    end
    

    그래서 당신은 어디서나 add_template_helper 지정하지 않는다.

  8. from https://stackoverflow.com/questions/1416978/how-to-use-my-view-helpers-in-my-actionmailer-views by cc-by-sa and MIT license