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 7160495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:24:31+00:00 2026-05-28T13:24:31+00:00

Thanks for looking. I am learning rails and am building a web app. I

  • 0

Thanks for looking. I am learning rails and am building a web app. I am trying to be “RESTful” and thinking in those terms. Therefore, I am looking at my data as a resource. Suppose I have a table called microposts and a column “votes” in which it counts how many people upvoted or downvoted. I have it working this way:

In the view:

<td>
    <%= form_for feed_item do |f| %>
        <div><%= hidden_field_tag 'todo', 'upvote' %></div>
        <div class="action"><%= f.submit "Up" %></div>
    <% end %>
</td>
<td>
    <%= form_for feed_item do |f| %>
      <div><%= hidden_field_tag 'todo', 'downvote' %></div>
      <div class="action"><%= f.submit "Down" %></div>
    <% end %>
<td>

When, a user clicks on the button, it calls the “update” method in the controller. Then, in the controller, I have this in update:

  def update
    @todo = params[:todo]
    if @todo == "upvote"
      Micropost.find(params[:id]).increment!(:votes)
      flash[:success] = "Increase micropost vote"
      redirect_to root_path
    elsif @todo == "downvote"
      Micropost.find(params[:id]).decrement!(:votes)
      flash[:success] = "Decrease micropost vote"
      redirect_to root_path
    else
      redirect_to root_path
    end
  end

I pass a hidden field to know if the button pressed is an “upvote” or a “downvote”. So, if I want to change any other micropost attribute, I need to have more and more if statements. I don’t think I am doing this right. What is the rails way to update attributes or a record in the DB?

Thanks much!

  • 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-05-28T13:24:32+00:00Added an answer on May 28, 2026 at 1:24 pm

    A more RESTful reworking is to define each of your actions as a separate route, GET if you must, POST ideally, to receive each of these types of calls:

    before_filter :load_micropost
    
    def load_micropost
      @micropost = Micropost.find(params[:id])
    end
    
    def upvote
      @micropost.increment!(:votes)
      flash[:success] = "Increase micropost vote"
      redirect_to root_path
    end
    
    def downvote
      @micropost.decrement!(:votes)
      flash[:success] = "Decrease micropost vote"
      redirect_to root_path
    end
    

    Don’t forget to create methods like load_micropost to handle routine loading of models. There’s no point in having several different calls strewn about your method that, at some point in the future, might require adjusting.

    You’d route these like this:

    map.resources :microposts,
      :member => {
        :upvote => :post,
        :downvote => :post
      }
    

    Another thing you might want to consider is adding a table that records who voted for what so you can audit and adjust if required. This makes it easy to unwind any fraudulent activity without having to reset the votes wholesale.

    You can also make simple links that POST if you ask for them:

    <%= link_to('Upvote', upvote_micropost_path(feed_item), :method => :post) %>
    

    This will take care of rendering whatever form elements are required.

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

Sidebar

Related Questions

For learning purposes, I'm looking for open source Rails projects that exemplify idiomatic usage
I'm looking to build an Android app(for learning) that searches for music files in
I am learning OpenXML. I have been looking for hours trying to find how
I'm learning how to do integration testin on my rails app. I started with
I am looking at learning to program a Java EE/SE web application with the
Thanks for looking. All sincerely helpful answers are voted up. I use a password
Thanks for looking, all helpful answers are voted up. This is my markup. I'm
Thanks for looking on this problem. I have a page that is totally valid
Thanks for looking at this question. I wanted to know how can I use
Looking at what's running and nothing jumps out. Thanks!

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.