Search code examples
ruby-on-railsdelayed-jobhelper

Call helper method from custom view Rails


In ApplicationHelper, I have method called finTime.

def finTime(time)
end

In delayed_job I have render_to_string function

class ExportJob < Struct.new(:time1,:time2, :survey_id)
   def perform
       ac = ActionController::Base.new()
   html = ac.render_to_string(:template => "pages/exportPdf.html.erb",:layout =>    "layouts/exportPdf",:formats => :html
   end
end

In exportPdf.html.erb I call the function finTime

<%= finTime(f.created_at) %>

I got the error: "ActionView::Template:Error: undefined method 'finTime' for #<#Class:0x6db8eb0>: 0x8321928>"

So I want to ask how I can call finTime method from exportPdf.html.erb in this case. I tried to use include and helper but it does not work. Thank you!


Solution

  • There's not a lot of information to go on here, but one possibility could be that in your definition there are no arguments - it's just finTime

    but in the call you give an argument of f.created_at - maybe that's causing the error, maybe not. what is the full error?

    related, the general practice for method names is snake case, so finTime should be fin_time.

    edit: Without seeing more of your code it's still difficult, for example

    <%= finTime(f.created_at) %>
    

    Is that what it's in your code? finTime() is a method, so it has to be called on a model - model.finTime(time)

    there's also an end parenthesis missing from this line:

    html = ac.render_to_string(:template => "pages/exportPdf.html.erb",:layout =>    "layouts/exportPdf",:formats => :html
    

    if both of those things are only problems in your question, and not in your code, try replacing finTime() with inspect or methods in your partial and see what happens. If there's still an error, you'll know you have to look elsewhere, and if not you'll get some information that might help you sort out the trouble. Good luck!