Allow People To Vote
Goals
Steps
Step 1: Add a new controller action for voting
def upvote @topic = Topic.find(params[:id]) @topic.votes.create redirect_to(topics_path) endStep 2: Add a new route for voting
resources :topics
resources :topics do member do post 'upvote' end endStep 3: Add the button to the view
<% @topics.each do |topic| %> <tr> <td><%= topic.title %></td> <td><%= topic.description %></td> <td><%= pluralize(topic.votes.count, "vote") %></td> <td><%= button_to '+1', upvote_topic_path(topic), method: :post %></td> <td><%= link_to 'Show', topic %></td> <td><%= link_to 'Edit', edit_topic_path(topic) %></td> <td><%= link_to 'Destroy', topic, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %>Step 4: Confirm your changes in the browser
Next Step:
Go on to Redirect To The Topics List After Creating A New Topic