2011-07-16 65 views
0

我陷入了这种情况。让我解释如下。我有一个电影模型,用户可以给一个评级的电影,他可以评论一部电影。所以我有分表。根据评论的数量,评分和平均评分e.t.c,我在电影控制器中编写了一个def point def结束方法。如何在控制器和其他控制器中调用方法

我有一个评论控制器和方法在它分别为创建评论和评级电影我写了一个方法在我的电影控制器创建评级。希望我明白直到现在。

所以我的问题陈述是。我想在制作评分和评论时在电影控制器中调用这个点数方法。

简要见解我粘贴我的代码在这里。

在电影控制器我的两个方法(收视率和点)

def rate 

    @movie= Movie.find(params[:movie_id]) 

    @count=0 

    @test= Rating.find(:last, :conditions=>['movie_id=?', @movie.id]) 

    unless @test.nil? || @test.blank? 

    @count= @test.count 


    end 

     if(params[:submitted]) 

      if @movie.ratings.create(:movie_id => params[:movie_id], :rate => params[:rating], :user_id=> current_user.id, :count=> @count+1) 

       points  

       flash[:success] = "Rating was successfully saved." 

      else 

       flash[:error] = 'Error. Rating was not saved. Try again later.' 

      end 

      redirect_to @movie 

      end 


end 

def points 

    @user= current_user 

    @movie= Movie.find(params[:movie_id]) 

    total_comments_by_user=Comment.find(:all, :conditions=>['user_id=? AND movie_id=?',@user.id,@movie.id]).count 

    user_who_rated= Rating.find(:all, :conditions=>['user_id=? AND movie_id=?',@user.id,@movie.id]) 

    @user_who_rated.each do |ur| 

     if @user=ur.user_id 

     @ratings_of_each_user=ur.rate 

    end 

     end 


    @over_all_comments_of_movie=Comment.find(:all, :conditions=> ['movie_id=?', @movie.id]).count 


@records=Rating.find(:all, :conditions => ['movie_id=?',@movie.id]).collect(& :rate) 

    unless @records.nil? || @records.blank? 

     @average_rating = 0.0 

     @records.each do |r| 

      @average_rating += r 

     end 

    @average_rating = @average_rating/@records.size 

end 


    unless @ratings_of_each_user.nil? || @ratings_of_each_user.blank? 

    if @total_comments_by_user.nil? || @total_comments_by_user.blank? 

    else 

    @[email protected]_of_each_user*@total_comments_by_user 

    @[email protected]_all_comments_of_movie* @average_rating 

     @total=0.0 

     @[email protected]_one.to_f/@div_two.to_f 


@find_user_in_points= Point.find(:all, :conditions=> ['user_id=? AND movie_id=?',current_user.id,@movie.id]) 

    if @find_user_in_points.nil? || @find_user_in_points.blank? 

    Point.create(:user_id=> current_user.id, :movie_id=>@movie.id, :points=> @total) 

    else 

     @find_user_in_points.each do |f| 

     if f.user_id= current_user.id 

      f. update_attributes(:points=> @total) 

     end 

    end 

     end 

     end 

    end 

    end 

现在评论控制器

def create 

    @user=current_user 

    @movie = Movie.find(params[:movie_id]) 

    @select_params= params[:choice_list] || [] 

    if @select_params.nil? || @select_params.blank? 

     @comment = @movie.comments.create(params[:comment])   


     @comment.update_attributes(:user_id=>@user.id, :commenter => @user.username) 

    else 

    @comment= @movie.comments.create(:commenter => "user", :body=>params[:comment][:body] , :movie_id=> @movie.id, :user_id=>@user) 

     end 

    redirect_to movie_path(@movie) 
    end 

def destroy 

    @comment = Comment.find(:all, :conditions=>['id=?',params[:id]]) 
    @comment.delete 
    redirect_to movie_path(@movie) 
    end 

我有我的看法文件和表格率影片的链接添加到评论电影。我只是想创建评分时创建评论并创建评论,以便我可以在内部执行我的积分计算并更新我的积分表。

请引导我。是否有可能从另一个控制器调用一个控制器中的方法,如果是这样,并告诉我如何在同一个控制器中调用一个方法。

+0

为什么你不会创建一个可以从两个控制器调用的共享方法?您真的不应该从控制器B调用控制器A ...控制器方法仅适用于浏览器请求。 – iwasrobbed

回答

0

请勿调用其他控制器。如果您需要共享的方法,无论是确保其在模型中(如果那是它的正确位置),或者在像ApplicationController中

而且共享控制器... unless @test.nil? || @test.blank? - String#blank?检查.nil?第一。那么.nil?没有必要。

相关问题