显示某个用户的帖子

时间:2013-03-21 18:13:59

标签: ruby-on-rails email controller dashboard

在我的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>

AND我的仪表板型号:

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不会工作!)

1 个答案:

答案 0 :(得分:3)

def show
  @user = User.find(params[:id])
  @posts = @user.posts

  #.....
end

这将仅显示该特定用户的帖子。

相关问题