1

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

4

1 回答 1

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

  #.....
end

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

于 2013-03-21T18:24:58.567 回答