在 R. Bates Railscasts #364 之后,我刚刚完成了 ARRS 的实施。我改变了它以适合我的应用程序,所以
用户在放映视图中对电影进行投票
这与 r 完全不同。贝茨在哪里
用户在索引视图中对俳句投票
启动后,按钮看起来很好,它们出现在显示视图上。
但是,当我单击一个时,会出现此错误
为 nil 调用 id,它会错误地为 4 - 如果你真的想要 nil 的 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
电影.rb
has_reputation :votes, source: :user, aggregated_by: :sum
用户.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>
路线.rb
resources :movies do
member { post :vote }
end