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 and add a table format
<p style="color: green"><%= notice %></p> <h1>Topics</h1> <div id="topics"> <table> <tr> <th> Topic </th> <th> Description</th> <th> Votes count</th> <th> </th> <th> </th> <th> </th> <th> </th> </tr> <% @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 %> </table> </div> <%= link_to "New topic", new_topic_path %>Step 4: Confirm your changes in the browser
Next Step:
Go on to Redirect To The Topics List After Creating A New Topic