Reputation: 8461
I currently have a basic controller and model that creates a Subscriber with the normal attributes(name, email, phone, etc...), but I also have a visit
attribute on the Subscriber. So I have a form that's rendered with the "new" action and save the input with the create action. << VERY BASIC >> The new feature I want to implement is create a new form that takes in only a phone_number and when the number is entered it update the visit attribute on the subscriber that that corresponds with that number. right now I'm struggling trying to figure this out with just one controller. I'll show my current controller and hopefully that will help for clarity.
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
@subscriber.save
redirect_to subscribers_visits_new_path(:subscriber)
else
render "new"
end
end
end
As you can see in the visit action I'm trying to implement this feature but I'm not sure where to go from here?
Upvotes: 0
Views: 73
Reputation: 3126
I suppose your search
page has a form where you type the number you want to search for. And the request comes to visit
action, where you'll increment the +1 to the visit count and then show the details of the number(subscriber).
def visit
#Comes request from search action
@subscriber = Subscriber.find_by_phone_number(params[:phone_number])
if @subscriber
@subscriber.visit += 1
@subscriber.save
redirect_to subscriber_path(@subscriber)
#It will show the details
else
flash[:alert] = "Didn't find with that number. Search again."
render :action => :search
end
end
Update:
Search logic
def search
#will render search.html.erb by convention
end
Inside search.html.erb
<%= form_tag({controller: "subscribers", action: "visit"}, method: "get") do %>
<%= text_field_tag :phone_number %>
<%= submit_tag "Submit" %>
<% end %>
So this will send params to visit
action, and after incrementing it will render show method. Hope this helps. Let me know if there is anything.
Upvotes: 1