我已經使用Ruby on Rails創建了一個博客應用程序,並且剛剛添加了一個身份驗證部分,並且它工作得很好。我現在正試圖通過我的應用程序來調整代碼,使其僅顯示與某個用戶相關的信息。current_user和帖子的評論 - 創建另一個關聯或循環帖子? - Ruby on Rails
當前,用戶has_many :posts
和Posts has_many :comments
。
創建帖子時,我成功地將user_id插入到帖子表中。此外,我成功地僅在/views/posts/index.html.erb視圖中顯示屬於特定用戶的帖子。我的問題是與評論。
例如,在主頁上登錄時,用戶將只能看到他們寫的帖子,而只能看到所有帖子上所有用戶的評論。這不是我想要的,需要一些方向來糾正。我只想顯示寫在所有登錄用戶帖子上的評論。
我是否需要創建關聯以使評論也屬於用戶?或者是否有一種方法可以調整我的代碼以簡單地循環顯示這些數據。
我已經把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 -%>
我究竟在哪裏使用@ user.comments?我是否在視圖或評論控制器中進行調整? – bgadoci 2010-04-29 20:42:27
好的,將控制器更改爲@comments = current_users.comments.find ...並且在顯示/評論時似乎工作正常,但如何使用上面的視圖代碼進行調整以顯示/posts/index.html.erb頁? – bgadoci 2010-04-29 20:44:18
@bgadoci:您應該將所有數據加載到控制器中並在視圖中顯示它。因此,在控制器中,您可以執行@user = current_user並在視圖中執行<%@ user.comments.each do | comment | %>'等等。或者在控制器中添加'@comments = @ user.comments'。在哪裏你可以做到這一點並不重要。 – klew 2010-04-29 20:46:12