Rails应用程序活动记录信誉系统错误来电显示

时间:2013-06-05 20:02:36

标签: ruby-on-rails activerecord railscasts rating-system

我刚刚完成了ARRS的实施,继R. Bates Railscasts#364之后。 我把它更改为适合我的应用程序,所以

  

用户在节目视图中对电影进行投票

这与r完全不同。贝茨的一个地方

  

用户在索引视图中对ha句进行投票

启动时,按钮看起来很好,它们出现在节目视图中。

当我点击一个时出现此错误

  

调用id为nil,错误地为4 - 如果你真的想要id为nil,请使用object_id

任何想法?

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 //电影

<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

2 个答案:

答案 0 :(得分:1)

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

问题可能在这里:

add_evaluation(:votes, value, current_user)

需要您没有的current_user。设计文件说你需要包括 控制器中的before_filter :authenticate_user!使用current_user助手。

使用宝石时要更加注意文档。

EIDT 在聊天解决此问题后,使用了acts_as_votable gem https://github.com/ryanto/acts_as_votable代替Active Record Reputation System

答案 1 :(得分:0)

这里的问题出在你的add_evaluation专栏

将其更改为 &#39; add_or_update_evaluation(:votes,value,current_user)&#39; 这将添加并更新评估部分。