Bitwise
Bitwise

Reputation: 8461

Two Create actions in one controller - Rails

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

Upvotes: 0

Views: 157

Answers (1)

Boltz0r
Boltz0r

Reputation: 980

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)

Upvotes: 1

Related Questions