0

Currently, I have a Subscribers controller that has a new, index and create action for subscribers. The function is very basic right now. I also have a subscriber model that takes in all the regular info including phone_number. I want a new form that will simply take in the subscribers phone_number and add 1 to the visit integer for that particular subscriber I have set up on the subscriber model. Here is my current code for clarity. Is this possible to basically update the subscriber that typed in their number and add 1 to the visit integer?

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[:success] = "Subscriber Has Been successfully Created"
   redirect_to new_subscriber_path(:subscriber)
  else
   render "new"
  end
 end

 def visit
   @subscriber = Subscriber.find_by_phone_number(params[:phone_number])
 if @subscriber
   @subscriber.visit += 1
   redirect_to subscribers_visits_new_path(:subscriber)
 else
   render "new"
 end
end

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
end
1
  • Is there no other information about a visit that you need to track? Is it just simply a counter? It feels to me like a visit should be a separate resource with a separate controller/model. Commented May 30, 2016 at 17:24

1 Answer 1

1

Of course it is. I would suggest creating with the url of your action (visit).

And then in your controller you just need to do

@subscriber.update_attribute(:visit, @subscriber.visit+1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.