Rusty Shackleford
Rusty Shackleford

Reputation: 5

Ruby on Rails - "undefined method `[]' for nil:NilClass" when using acts_as_votable gem

I'm trying to create a Reddit clone in which users and upvote and downvote posts. I've installed and ran the necessary migrations for the acts_as_votable gem (https://github.com/ryanto/acts_as_votable):

# app/models/user.rb
class User < ApplicationRecord
    has_many :posts
    devise :database_authenticatable, :registerable, :trackable, :validatable
    ...
    acts_as_voter
end

# app/models/post.rb
class Post < ActiveRecord::Base
    belongs_to :user
    ...
    acts_as_votable
end

I should also mention that I'm using single table inheritance to simplify working with each type of post:

# app/models/text_post.rb
class TextPost < Post
...
end

# app/models/link.rb
class Link < Post
...
end

I've tried to implement upvote/downvote functionality (http://www.mattmorgante.com/technology/votable):

# config/routes.rb
...
resources :posts do
    member do 
        put "like", to: "posts#upvote"
        put "dislike", to: "posts#downvote"
    end
    ...
end
...

# app/controllers/posts_controller.rb
class PostsController < ApplicationController
    before_action :authenticate_user!, except: :index
    ...
    def upvote
        @post = Post.find(params[:id])
        @post.upvote_by current_user
        redirect_to :back
    end

    def downvote
        @post = Post.find(params[:id])
        @post.downvote_by current_user
        redirect_to :back
    end
end

# app/views/posts/index.html.erb
...
<% @posts.each do |post| %>
    ...
    <%= link_to like_post_path(post), method: :put do %>
        <i class="fa fa-arrow-up"></i>
    <% end %>
    ...
    <%= link_to dislike_post_path(post), method: :put do %>
        <i class="fa fa-arrow-down"></i>
    <% end %>
    ...
<% end %>
...

But when I try to vote on a post, I get

NoMethodError in PostsController#upvote

undefined method `[]' for nil:NilClass

on this line in my controller:

@post.upvote_by current_user

Even if I attempt it manually in the console without using current_user, I get the same error:

irb(main):001:0> user = User.first
...
irb(main):002:0> post = Post.first
...
irb(main):003:0> post.upvote_by user
...
Traceback (most recent call last):
    1: from (irb):3
NoMethodError (undefined method `[]' for nil:NilClass)

I'm unsure if there is something wrong with my code or if it's perhaps a compatibility issue as I'm using Rails 5.2.0 and the GitHub page for acts_as_votable lists only 5.0 and 5.1 as supported versions.

If anyone could shed some light on this, it would be greatly appreciated.

Upvotes: 0

Views: 1054

Answers (2)

Joe Bloggos
Joe Bloggos

Reputation: 889

Hi Its been a long time but I think you might not have been setting your post in the controller.

NoMethodError in PostsController#upvote
undefined method `[]' for nil:NilClass

The error is telling you that you are trying to vote on a class that has not been set. you have to set the class in the controller

before_action :set_post, only: [:show, :edit, :update, :destroy, :upvote, :downvote]

Upvotes: 0

arieljuod
arieljuod

Reputation: 15838

upvote_by seems to be an alias of vote_up, maybe you are not using the same gem version from the tutorial?

https://github.com/ryanto/acts_as_votable/blob/599995f7ec5aa0f8a04312768fc956e9003d32d4/lib/acts_as_votable/votable.rb#L15

Try using vote_up instead, since it looks like the original method, it should work on versions where the upvote_by alias was not set.

Upvotes: 0

Related Questions