title: Moving this action that works in the show view into the index view (Rails)?
body: <p>This is how the user votes the current post that it is showing:</p>
  
  <p><strong>controllers/votes_controller.rb:</strong></p>
  
  <pre><code>class VotesController &lt; ApplicationController
    def vote_up
      @post = Post.find(params[:id])
  
      if @post.votes.exists?(:user_id =&gt; current_user.id)
        @notice = 'You already voted'
      else
        @vote = @post.votes.create(:user_id =&gt; current_user.id, :polarity =&gt; 1)
      end
  
      respond_to do |format|
        format.js
      end
    end
  </code></pre>
  
  <p>This updates the total number of votes with Ajax:</p>
  
  <p><strong>vote_up.js.erb:</strong></p>
  
  <pre><code>&lt;% unless @notice.blank? %&gt;
    alert("&lt;%= @notice %&gt;");
  &lt;% end %&gt;
  
  &lt;% unless @vote.blank? %&gt;
    $('.post-&lt;%=@post.id%&gt; span.vote-count').html('&lt;%= @post.votes.count %&gt;');
    $('.post-&lt;%=@post.id%&gt; div.voted-user').html('&lt;% @post.votes.each do |vote| %&gt;&lt;%= link_to vote.user.username, vote.user %&gt;&lt;% end %&gt;');
  &lt;% end %&gt;
  </code></pre>
  
  <p>This is the voting link in the show view:</p>
  
  <p><strong>views/posts/show.html.erb:</strong></p>
  
  <pre><code>&lt;%= link_to "Vote Up", vote_up_path(@post), :remote =&gt; true, :class =&gt; "vote-up" %&gt;&lt;br /&gt;
  </code></pre>
  
  <p>and the routing for the action:</p>
  
  <p><strong>routes.rb:</strong></p>
  
  <pre><code>get 'votes/:id/vote_up' =&gt; 'votes#vote_up', as: 'vote_up'
  </code></pre>
  
  <p>This is how all the posts are displayed in the index view:</p>
  
  <p><strong>views/posts/index.html.erb:</strong></p>
  
  <pre><code>&lt;% @posts.each do |post| %&gt;
      &lt;h2&gt;Title: &lt;%= post.title %&gt;&lt;/h2&gt;
      &lt;p&gt;Author: &lt;%= post.user.username %&gt;&lt;/p&gt;
      &lt;p&gt;Created At: &lt;%= post.created_at %&gt;&lt;/p&gt;
      &lt;p&gt;Content: &lt;%= post.content %&gt;&lt;/p&gt;
      &lt;p&gt;Votes: &lt;%= post.total_votes %&gt;&lt;/p&gt;
      &lt;p&gt;Comments: &lt;%= post.comments_count %&gt;&lt;/p&gt;
      &lt;ul&gt;
        &lt;li&gt;&lt;%= link_to 'Show', post %&gt;&lt;/li&gt;
        &lt;li&gt;&lt;%= link_to 'Edit', edit_post_path(post) %&gt;&lt;/li&gt;
        &lt;li&gt;&lt;%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %&gt;&lt;/li&gt;
      &lt;/ul&gt;
      &lt;br /&gt;
    &lt;% end %&gt;
  </code></pre>
  
  <p>I would like to add the voting link in the <code>index.html.erb</code> view for each post or find a way of triggering the <code>vote_up</code> action for each post in the index view. Any suggestions to accomplish that?</p>