在我的Rails應用程序中,我需要在該用戶所創建的用戶控制面板的帖子上顯示。某個用戶的顯示帖子
儀表板控制器:
class DashboardController < ApplicationController
def index
@users = User.all
@user = User.find(params[:id])||current_user
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
def show
@user = User.find(params[:id])
@post = Post.all(:order => "created_at DESC")
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
end
我的主頁 '顯示視圖'
<div class="dash-well">
<div class="gravatar-dashboard">
<%= image_tag avatar_url(@user), :class => 'gravatar-pos-fix gr-dash-mar-top' %>
<h1 class="nuvo wtxt"><%= @user.username.capitalize %></h1>
<h3 class="nuvo wtxt"><%= @user.motto %></h3>
</div>
</div>
<div class="dash-well-status">
<% @post.each do |post| %>
<div class="post">
<div class="tstamp">
<%= image_tag avatar_url_small(post.user), :class => 'gravatar' %>
<strong>Posted <%= time_ago_in_words(post.created_at) %> ago by <%= post.user.username %></strong>
</div>
<div class="status"><%= post.status %></div>
</div>
<% end %>
</div>
和我的儀表盤型號:
class Dashboard < ActiveRecord::Base
attr_accessible :status, :author, :email, :username, :id, :user_id, :user, :website, :bio, :skype, :dob, :age
has_many :posts
belongs_to :user
end
因此,沒有人知道我怎麼可能只顯示來自用戶儀表板的帖子(用戶儀表板可以從任何用戶訪問,所以current_user不會工作!)
用戶和帖子之間是否有任何關係? – 2013-03-21 18:17:42
@SagarBommidi用戶has_many:帖子和帖子belongs_to:用戶 – coreypizzle 2013-03-21 18:20:12
我想你的帖子有'user_id'這個欄目,爲什麼不用它來代替'Post.all()'? – Gerep 2013-03-21 18:20:24