Method to render how many days remain in membership - Ruby on Rails -
currently have method called expiration_date
calls created_at , adds 1 year date. make method says how many days left until expiration_date
, call on view. i'll show model code clarity , make more sense.
model
class subscriber < activerecord::base validates :first_name, presence: true validates :last_name, presence: true validates :email, presence: true validates :phone_number, presence: true def date_joined created_at.strftime("%-m/%-d/%-y") end def expiration_date (created_at + 1.year).strftime("%-m/%-d/%-y") end def days_till_expired #code end end
controller
class subscriberscontroller < applicationcontroller def index @subscriber = subscriber.all end def new @subscriber = subscriber.new end def create @subscriber = subscriber.create(subscriber_params) if @subscriber.save flash[:notice] = "subscriber has been created" redirect_to new_subscriber_path(:subscriber) else render "new" end end def search @subscriber = subscriber.new end def visit @subscriber = subscriber.find_by_phone_number(params[:phone_number]) if @subscriber @subscriber.visit =+ 1 @subscriber.save redirect_to subscribers_search_path(:subscriber) else render "search" end end end
i have view form user type in phone_number , flash notice aware them how time have left. think need grab user params of phone number when it's sent in. make sense?
how this
def days_till_expiration ((created_at + 1.year) - datetime.now).to_i / 1.day end