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

  • Home
  • SEARCH
  • 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 8815237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:19:33+00:00 2026-06-14T04:19:33+00:00

What’s the problem?: I am creating a general blog, and working to create the

  • 0

What’s the problem?:

I am creating a general blog, and working to create the essential features.
The problem is that the view (how many people have seen a particular post) is not shown correctly.

The state of art:.

I have two controllers/models at this point: Post and Comment. I will provide you with my schema to help you understand the situation better.

create_table "comments", :force => true do |t|
t.string   "name"
t.text     "body"
t.integer  "like"
t.integer  "hate"
t.integer  "post_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "comments", ["post_id"], :name => "index_comments_on_post_id"

create_table "posts", :force => true do |t|
t.string   "name"
t.text     "content"
t.integer  "view"
t.integer  "like"
t.integer  "hate"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

What I have done:
In the Post Controller, I tried to initialize the views by setting it to 0 whenever someone creates a post.

# POST /posts
# POST /posts.json
def create
@post = Post.new(params[:post])
@view = @post.view
@view = 0

respond_to do |format|
  if @post.save
    format.html { redirect_to posts_path, notice: 'success.' }
    format.json { render json: @post, status: :created, location: @post }
    @view = @post.view
    @view = 0
  else
    format.html { render action: "new" }
    format.json { render json: @post.errors, status: :unprocessable_entity }
  end

end

And the post should be able to show its number of views. So in the show.html.erb file of post, I added the part below as well.
<p>
<b>views:</b>
<%= @post.view %>
</p>

How it doesn’t work:
The number of views is simply not shown at all. I checked the database, and the column was blank even if I attempted to initialize it to 0 whenever a user adds a post. I’m guessing there is a problem with the way I try to access the “view” variable? Is it not right to access it by doing

@view = @post.view?

I’m aware of the fact that I’ll have to consider if a user that’s already seen the post revisits, but I’m not there yet until I know exactly how to access the view variable/initialize/increment it.

I really appreciate your help in advance!

-Max

  • 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-14T04:19:35+00:00Added an answer on June 14, 2026 at 4:19 am

    To initialize Post’s view attribute to zero, before invoking @post.save (as a side effect of the if statement):

    @post.view = 0
    

    Then in your controller actions that render the post you will need to update a Post’s view attribute by 1. This will have problems if multiple people access the page at the same time between database reads and writes but it will at least get you moving in the right direction.

    Assignments of @view here to values after saving the database are accomplishing nothing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm trying to create an if statement in PHP that prevents a single post
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.