Cameron Bass Cameron Bass - 6 months ago 9
Ruby Question

undefined local variable or method `days_till_expired'

Currently I have a method called

days_till_expired
defined in my
subscriber
model. I'm trying to call it in the controller but it doesn't seem to be working? I must be missing something obvious because this should be a simple implementation. I'll post my code for clarity.

MODEL METHOD:

def days_till_expired
((created_at + 1.year) - DateTime.now).to_i / 1.day
end


CONTROLLER:

def visit
@subscriber = Subscriber.find_by_phone_number(params[:phone_number])
if @subscriber
@subscriber.visit =+ 1
@subscriber.save
flash[:notice] = "Thank You! You have #{days_till_expired} until renewal"
redirect_to subscribers_search_path(:subscriber)
else
render "search"
end
end


As you can see I'm simple trying to call the method with interpolation inside the flash notice but I keep getting this ERROR:

enter image description here

Answer

You are not calling to the methods, if you have days_till_expired on your model you forgot to use your model @subscriber.days_till_expired

flash[:notice] = "Thank You! You have #{@subscriber.days_till_expired} until renewal"

If I were you, I would try to move that counter logic to the model.