0

I know this should be easy, but can't really find a straight answer on how im supposed to be calling a method from html.erb.

On my edit page, I want to run a simple time calculation (current time - start time from DB).

I trying to call the method using

Time: <% totalhours(@timetracker.time_start, @time_now) %>

In my timetracker contoller edit action I have

  def edit
   
    def totalhours(starttime, endtime)
      totalhourscalc = (Time.parse(endtime) - Time.parse(starttime) / 3600)
      p totalhourscalc
    end

  end
'''

The error im getting is "undefined method `totalhours' for #<ActionView::Base:0x007f244820cc90>    {highlighted} Time: <% totalhours(@timetracker.time_start, @time_now) %>" {/highlighted}

I have tried multuple variations and spent last couple of hours trying to find a simple answer. Help would be greatly appreciated on what im doing wrong.

Thank you in advance!!!!!!!

  • 1
    You can call helpers, you can call objects on a method, you can access instance values you set in the action; you can't call arbitrary things you define. Off you're just going to set a value in the controller, just expose there *result* as an instance variable. But define the method as a private method if it's not intended to be a general purpose helper. – Dave Newton May 31 '21 at 20:34
  • 1
    Define your totalhours method outside of edit, but still in the Controller class. Then add `helper_method :totalhours`. Is this what you want to do? Please see this: https://stackoverflow.com/questions/8906527/can-we-call-a-controllers-method-from-a-view-as-we-call-from-helper-ideally – esotericpig Jun 01 '21 at 14:33
  • Both of these answers were extremely helpful. Thank you both. – NoYellAtmonkeys Jun 02 '21 at 05:49

0 Answers0