1

データベースにコンテンツを保存するためにパーシャルをレンダリングするヘルパー クラスがあります。

正常に動作しますが、ビューに url ヘルパーによって提供される url が含まれている場合

<%= link_to "Show project", projects_url, class: "button" %>

次の例外をスローします

# の未定義のローカル変数またはメソッド projects_url

NotificationRender という名前のヘルパーでレンダリングするコードは次のとおりです。

def render(options)
    viewer = ActionView::Base.new()
    viewer.view_paths = ActionController::Base.view_paths
    viewer.extend ApplicationHelper    
    viewer.render options
end

このヘルパーをクラス通知に含めます

class Notification < ActiveRecord::Base
  include NotificationRender

  .....
  def self.create_for(user, event, params)   
      template = "notifications/_email_project"
      list_params = {:template => template,:locals => params}
      notification = Notification.new
      notification.message = notification.render(list_params) #here I render the partial
      notification.subject = I18n.t "subject.#{event}"
      notification.to = user.email     
      notification.save
 end

end
4

0 に答える 0