Question

I have just finished implementing ARRS, following R. Bates Railscasts #364. I changed it to fit my app, so

a user votes on a movie in show view

Which is quite different to r. bates' one where

a user votes on a haiku in index view

And upon launching, the buttons look fine and they appear on the show view.

yet when i click on one this error appears

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

Any ideas anyone?

Movies_controller.rb

class MoviesController < ApplicationController



  # GET /movies
  # GET /movies.json
  def index
    @search = Movie.search(params[:q])
    @movies = Movie.all


  end

  # GET /movies/1
  # GET /movies/1.json
  def show

      @movies = Movie.find_with_reputation(:votes, :all, order: 'votes desc')
      @search = Movie.search(params[:q])
    @movie = Movie.find(params[:id])


  end




    def search
        @search = Movie.search(params[:q])
        @movies = @search.result

        respond_to do |format|
            format.html # index.html.erb
            format.json { render json: @movies }
        end
    end

  # GET /movies/new
  # GET /movies/new.json
  def new
      @search = Movie.search(params[:q])
    @movie = Movie.new


  end

  # GET /movies/1/edit
  def edit
      @search = Movie.search(params[:q])
    @movie = Movie.find(params[:id])
  end

  # POST /movies
  # POST /movies.json
def create
    @search = Movie.search(params[:q])
    @movie = Movie.new(params[:movie])

    respond_to do |format|
      if @movie.save
        format.html { redirect_to @movie, notice: 'Movie was successfully created.' }
        format.json { render json: @movie, status: :created, location: @movie }
      else
        format.html { render action: "new" }
        format.json { render json: @movie.errors, status: :unprocessable_entity }
      end
    end
  end


  # PUT /movies/1
  # PUT /movies/1.json
 def update
     @search = Movie.search(params[:q])
    @movie = Movie.find(params[:id])

    respond_to do |format|
      if @movie.update_attributes(params[:movie])
        format.html { redirect_to @movie, notice: 'Movie was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @movie.errors, status: :unprocessable_entity }
      end
    end
  end


  # DELETE /movies/1
  # DELETE /movies/1.json
  def destroy

    @movie = Movie.find(params[:id])
    @movie.destroy
  end



def vote
  value = params[:type] == "up" ? 1 : -1
  @movie = Movie.find(params[:id])
  @movie.add_evaluation(:votes, value, current_user)
  redirect_to :back, notice: "Thank you for voting!"
end

end

Movie.rb

has_reputation :votes, source: :user, aggregated_by: :sum

User.rb

has_reputation :votes, source: {reputation: :votes, of: :movies}, aggregated_by: :sum

show.html.erb //movies

<div class="ratings">

  <em>
    <%= pluralize @movie.reputation_for(:votes).to_i, "vote" %>
    | <%= link_to "up", vote_movie_path(@movie, type: "up"), method: "post" %>
    | <%= link_to "down", vote_movie_path(@movie, type: "down"), method: "post" %>
  </em>
</div>

routes.rb

 resources :movies do
    member { post :vote }
  end
Was it helpful?

Solution

def vote
  value = params[:type] == "up" ? 1 : -1
  @movie = Movie.find(params[:id])
  @movie.add_evaluation(:votes, value, current_user)
  redirect_to :back, notice: "Thank you for voting!"
end

the problem might be here:

add_evaluation(:votes, value, current_user)

requires current_userthat you don't have. Devise documentations says you need to include before_filter :authenticate_user! in the conotroller to use the current_user helper.

Pay more attention to documentations when using a gem.

EIDT after chatting to solve this issue the acts_as_votable gem was used https://github.com/ryanto/acts_as_votable instead of Active Record Reputation System

OTHER TIPS

here the problem is in your add_evaluation line

change it to 'add_or_update_evaluation(:votes, value, current_user)' this will add and also update the evaluation part.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top