Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:42:22+00:00 2026-06-13T17:42:22+00:00

I have an Advert model that basically has an image, title and a url.

  • 0

I have an Advert model that basically has an image, title and a url. I use CarrierWave for the image. Everything works well, but I am wanting to count impressions and clicks.


So far, I added two integer columns: impressions, clicks

I have a pages controller and have set an @advert variable that changes depending on the page. After reading this question I am able to increment impressions:

 def index  
   @advert = Advert.sample  
   @advert.increment! :impressions  
 end   

I am having trouble incrementing the clicks | Below is the code I need to modify so that when a user clicks on the image_tag they are taken to the URL as normal, but that the @advert increments the clicks column. Here is my current code (I use HAML and this is code is in a file: _advert.html.haml):

  = link_to image_tag("#{advert.foto_url.to_s}",:alt => "an image ad linking to #{advert.url}", :title => advert.title), advert.url, :target => "blank"

What is the easiest, cleanest way to successfully increment! the clicks attribute on the Advert when someone clicks on the link above?

Update:

I am using the suggestion from Aaron below, but it leads to two new concerns:

  • The user will see a different link when they hold their mouse over the image than the final link they will end up at. I would prefer to have them see the final URL they will end up if they click rather than Redirecting simply to count their click.

  • If someone pulls up the advert url (eg: /adverts/1) directly the counter would increment! without a click.

I actually didn’t have a show view for Advert, but added it to get this done. So to be clear this works, but it feels like it violates best practices. Thank you though to Aaron.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:42:23+00:00Added an answer on June 13, 2026 at 5:42 pm

    I think the easiest way would be to make the advert image link to the advert show action, which could increment the counter and then redirect to the advert url.

    In your link, change advert.url to advert_path(advert) (and set up the necessary route) then in adverts_controller:

    def show
      @advert = Advert.find(params[:id])
      @advert.increment! :clicks
      redirect_to @advert.url
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a GeoDjango project that has a manager model like this; class AdvertManager(models.GeoManager):
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
I have an Advert model. Each Advert belongs to an Area. In an attempt
I have the following Models and ViewModels (edited for brevity): public class Advert {
I have two MySQL tables structed as follows; advert +-------------------+--------------+ | Field | Type
i have an upload script which takes a timestamp when an advert is uploaded,
I'm learning HAML and I have a problem with from_for %b New Advert! -
For the past month, I have noticed that the adverts that appear in my
How to get specific column from model in Django( SELECT city FROM advert )
I have two tables, Campaign and Advert with a one to many relationship. During

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.