current_user和帖子评论 - 创建另一个关联或循环帖子? - Ruby on Rails

时间:2010-04-29 20:28:02

标签: ruby-on-rails ruby authentication associations

我使用Ruby on Rails创建了一个博客应用程序,并且刚刚添加了一个身份验证部分,它运行良好。我现在正在尝试返回我的应用程序来调整代码,使其仅显示与特定用户关联的信息。

目前,用户has_many :postsPosts has_many :comments

创建帖子后,我成功将user_id插入到post表中。此外,我成功只在登录/views/posts/index.html.erb视图时显示属于某个用户的帖子。我的问题在于评论。

例如,在主页上,当用户登录时,用户只会看到他们写过的帖子,但会看到所有帖子上所有用户的评论。这不是我想要的,需要一些方向来纠正。我只想显示写在所有登录用户帖子上的评论。

我是否需要创建关联,以便评论也属于用户?或者有没有办法调整我的代码,只需循环通过post来显示这些数据。

我已将下面的PostsController,CommentsController和/posts/index.html.erb的代码以及我的视图代码放入,但如果需要,会发布更多代码。

class PostsController < ApplicationController

  before_filter :authenticate

  auto_complete_for :tag, :tag_name
  auto_complete_for :ugtag, :ugctag_name

  def index
    @tag_counts = Tag.count(:group => :tag_name, 
       :order => 'count_all DESC', :limit => 20)
       conditions, joins = {}, :votes

    @ugtag_counts = Ugtag.count(:group => :ugctag_name, 
       :order => 'count_all DESC', :limit => 20)
       conditions, joins = {}, :votes

    @vote_counts = Vote.count(:group => :post_title, 
          :order => 'count_all DESC', :limit => 20)
          conditions, joins = {}, :votes


       unless(params[:tag_name] || "").empty?
         conditions = ["tags.tag_name = ? ", params[:tag_name]]
         joins = [:tags, :votes]
       end
       @posts= current_user.posts.paginate(
                 :select => "posts.*, count(*) as vote_total", 
                 :joins => joins, 
                 :conditions=> conditions, 
                 :group => "votes.post_id, posts.id ", 
                 :order => "created_at DESC",
                 :page => params[:page], :per_page => 5)
        @popular_posts=Post.paginate(
                 :select => "posts.*, count(*) as vote_total", 
                 :joins => joins, 
                 :conditions=> conditions, 
                 :group => "votes.post_id, posts.id", 
                 :order => "vote_total DESC",
                 :page => params[:page], :per_page => 3)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @posts }
      format.json { render :json => @posts }
      format.atom
    end
  end

  def show
    @post = Post.find(params[:id])


    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @post }
    end
  end

  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @post }
    end
  end

  def edit
    @post = Post.find(params[:id])
  end

  def create
    @post = current_user.posts.create(params[:post])

    respond_to do |format|
      if @post.save
        flash[:notice] = 'Post was successfully created.'
        format.html { redirect_to(@post) }
        format.xml  { render :xml => @post, :status => :created, :location => @post }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        flash[:notice] = 'Post was successfully updated.'
        format.html { redirect_to(@post) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end


  def destroy
    @post = Post.find(params[:id])
    @post.destroy


    respond_to do |format|
      format.html { redirect_to(posts_url) }
      format.xml  { head :ok }
    end
  end
end

CommentsController

class CommentsController < ApplicationController

  before_filter :authenticate, :except => [:show, :create]

  def index

    @comments = Comment.find(:all, :include => :post, :order => "created_at DESC").paginate :page => params[:page], :per_page => 5

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @comments }
      format.json { render :json => @comments }
      format.atom
    end
  end

  def show
     @comment = Comment.find(params[:id])

     respond_to do |format|
       format.html # show.html.erb
       format.xml  { render :xml => @comment }
     end
   end

   # GET /posts/new
   # GET /posts/new.xml

   # GET /posts/1/edit
   def edit
     @comment = Comment.find(params[:id])
   end

   def update
     @comment = Comment.find(params[:id])

     respond_to do |format|
       if @comment.update_attributes(params[:comment])
         flash[:notice] = 'Comment was successfully updated.'
         format.html { redirect_to(@comment) }
         format.xml  { head :ok }
       else
         format.html { render :action => "edit" }
         format.xml  { render :xml => @comment.errors, :status => :unprocessable_entity }
       end
     end
   end


 def create
   @post = Post.find(params[:post_id])
   @comment = @post.comments.build(params[:comment])

   respond_to do |format|
     if @comment.save
       flash[:notice] = "Thanks for adding this comment" 
       format.html { redirect_to @post }
       format.js
     else
       flash[:notice] = "Make sure you include your name and a valid email address" 
       format.html { redirect_to @post }


   end
 end
end 







 def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy



    respond_to do |format|
      format.html { redirect_to Post.find(params[:post_id]) }
      format.js

    end
  end

end

查看评论代码

<% Comment.find(:all, :order => 'created_at DESC', :limit => 3).each do |comment| -%>
                <div id="side-bar-comments">
                    <p>
                        <div class="small"><%=h comment.name %> commented on:</div>
                        <div class="dark-grey"><%= link_to h(comment.post.title), comment.post %><br/></div>
                        <i><%=h truncate(comment.body, :length => 100) %></i><br/>
                        <div class="small"><i> <%= time_ago_in_words(comment.created_at) %> ago</i></div>
                    </p>
                </div>
            <% end -%>

2 个答案:

答案 0 :(得分:1)

我相信你可以在user模型

上设置另一种关系
has_many :comments, :through => :posts

然后是用户@user.comments

答案 1 :(得分:1)

首先,不要搞乱MVC。行:

Comment.find(:all, :order => 'created_at DESC', :limit => 3)

应该在控制器中:

@comments = Comment.find(:all, :order => 'created_at DESC', :limit => 3)

并在视野中:

<% @comments.each do |comment| %>

甚至更好的偏见:

<%= render :partial => "comment_item", :collection => @comments %>

它会迭代所有@comments

接下来,如果要显示分配给帖子的所有评论,那么关联帖子has_many评论就足够了。像这样使用它:

# controller
@posts = current_user.posts(:include => :comments)

# view
<% @posts.each do |post| %>
  <%=h post.title %> - Comments: <br />
  <% post.comments.each do |comment| %>
    <%=h comment.body %>
  <% end %>
<% end %> 

如果您只想显示current_user发布的评论,那么您的所有评论都应填写user_id字段。并以与显示用户帖子相同的方式使用它。