Reputation: 1111
I have a post page with a consistent <a href="">
link button in it. I need to show link clicked number for each post. I already have table columns as
t.integer "count_click"
How can I make this counter in the simplest form possible?
<%= link_to @post.reference_title, @post.reference_url, target: :_blank %>
I probably can make it happen with a simple SQL query in the Controller, right? But I have no idea how to trigger it with click
Thank you!
Upvotes: 0
Views: 410
Reputation: 1312
For a basic implementation, as per my comments, I'd do it in the controller action. I guess I would do it all in the same action for your example.
For your original question there were 2 counters:
t.integer "count_click"
t.integer "count_view"
I'd try to add a param to the link itself that would pass when clicking it:
<%= link_to @post.reference_title, reference_url(@post, clicked: true), target: :_blank %>
For a basic implementation you could increment like this:
def reference
# Increment the view counter whenever page loads.
Post.increment_counter(:count_view, @post.id)
# Increment the click counter if the link was clicked based on param.
Post.increment_counter(:count_click, @post.id) if params[:clicked] == "true"
end
Upvotes: 1