2013-06-05 42 views
1

我剛剛完成ARRS的實施,後面R.貝茨Railscasts#364。 我在放映視圖Rails應用程序活動記錄聲譽系統錯誤來電ID

這是與R完全不同的改變它適合我的應用程序,所以

用戶選票上的影片。貝茨之一,在索引視圖

而且在啓動上俳句

用戶投票,按鈕看起來不錯,他們在展會上亮相視圖。

然而當我點擊一個這個錯誤出現

名爲id的零,這將被錯誤地4 - 如果你真的想零的ID,請使用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 
+0

你可以添加相關的代碼到你的問題? – fontno

+0

Okk做到了嗎?再也沒有代碼或者是一切嗎? – PMP

+0

你可以發佈你的'routes.rb'嗎?跟蹤中的錯誤是什麼? – fontno

回答

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!在conotroller使用current_user幫手。

使用寶石時要多留意文檔。

EIDT聊天來解決這個問題acts_as_votable寶石之後再使用的 https://github.com/ryanto/acts_as_votable代替Active Record Reputation System

+0

我現在得到這個錯誤----未定義的方法'add_evaluation'for# ----點擊向上或向下 – PMP

+0

我以current_user身份登錄,並且只有一個before_filter:authenticate_user !,只有:[:vote] – PMP

+0

您必須通過錯誤參數查找影片,可能影片作爲movie_id傳遞,而不僅僅是id,所以你需要使用@movie = Movie.find(params [movie_id]) – rmagnum2002

0

這裏的問題是在你的add_evaluation線

改變它 「add_or_update_evaluation(:票,值,current_user)' 這將添加並更新評估部分。