Search code examples
ruby-on-railscontrollervoting-system

Issues with update action using acts_as_votable


I'm currently getting the votes to hit the database but I'm now having issues getting the update action to work in my controller. The votes don't record with the update action but do without out it. However, I then get a missing template error for Pits#update. Any help is appreciated. Thanks.

Terminal Error

Started PUT "/pits/3" for 127.0.0.1 at 2014-08-21 11:38:25 -0500
Processing by PitsController#update as JS
  Parameters: {"id"=>"3"}
  User Load (0.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 4  ORDER BY "users"."id" ASC LIMIT 1
  Pit Load (0.2ms)  SELECT  "pits".* FROM "pits"  WHERE "pits"."user_id" = ? AND "pits"."id" = ? LIMIT 1  [["user_id", 4], ["id", 3]]
Completed 404 Not Found in 64ms

ActiveRecord::RecordNotFound (Couldn't find Pit with 'id'=3 [WHERE "pits"."user_id" = ?]):
  app/controllers/pits_controller.rb:37:in `update'

I currently have

Pits Controller

class PitsController < ApplicationController

def new
  @pit = Pit.new
end

def index
  @pit = Pit.all
  @user = User.find_by(params[:id])
  @pits = Pit.paginate(:page => params[:page]).order('created_at ASC').group_by { |pit| pit.created_at.strftime("%B %Y") }
end




def create
  @user = current_user
  @pit = current_user.pits.create(pit_params)
    if @pit.save
      redirect_to @pit
    else
      render 'new'
    end
end



def show
  @pit = Pit.find(params[:id])
end

def edit
end

def update
   @user = current_user
   @pit = current_user.pits.find(params[:id])
   if @pit.update_attributes(pit_params)
      redirect_to @pit
end
end

private

def pit_params
    params.require(:pit).permit(:topic, :summary, :image, :video_url, :author, :user_id)
end

end

Comments Controller

class CommentsController < ApplicationController


 def create
  @pit= Pit.find(params[:pit_id])
  @comment = @pit.comments.build(comments_params)
  @comment.user = current_user
  @comment.save

  redirect_to pit_path(@pit)
end

  def destroy
    @pit = Pit.find(params[:pit_id])
    @comment = @pit.comments.find(params[:id])
    @comment.destroy
    redirect_to pit_path(@pit)
end

def upvote
  @pit = Pit.find(params[:pit_id])
  @comment = @pit.comments.find(params[:comment_id])
  @comment.upvote_by current_user
  redirect_to pit_path(@pit)
end

def downvote
  @pit = Pit.find(params[:pit_id])
  @comment = @pit.comments.find(params[:comment_id])
  @comment.downvote_from current_user
  redirect_to pit_path(@pit)
end

def update
end




def show  
end

  private

def comments_params
    params.require(:comment).permit(:body, :user_id, :votable, :voter, :vote_scope)
end

end

_comment.html.erb

  <div class = "well">
    <p>
    <strong>Comment:</strong>
    <%= comment.body %>
    <p>posted by: <%= comment.user.name %></p>
       <%= link_to "Like", pit_comment_like_path(@pit, comment), method: :put , :remote => true %>
       <%= link_to "Dislike", pit_comment_dislike_path(@pit, comment), method: :put, :remote => true %>
  </p>

  <p>
    <%if comment.user == current_user %>

     <%= link_to 'Destroy Comment', [@pit, comment],
                 method: :delete,
                 data: { confirm: 'Are you sure?' } %>
    <% end %>             
  </p>
  </div>

Solution

  • Not sure about everything you have going on here, but I suspect the error has to do with use current_user to find the pit. If the current_user is not the user_id for the pit, it won't find any pit (exactly your error).

    Try adjusting to this and it should be able to find the pit propperly.

    def update
       @pit = Pit.find(pit_params[:id])
       if @pit.update_attributes(pit_params)
          redirect_to @pit
    end