简体   繁体   English

显示某个用户的帖子

[英]Display posts by a certain user

In my rails app, I need to display on a users 'dashboard' posts in which that user had made. 在我的rails应用程序中,我需要在用户的仪表板上显示该用户所做的帖子。

Dashboard Controller : 仪表板控制器:

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

My Dashboard 'Show View' 我的仪表板'显示视图'

<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 My Dashboard Model : 和我的仪表板型号:

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

So, does anyone know how I could display only the posts from the users dashboard (The users dashboard is accessible from any user so, current_user wont work!) 那么,有没有人知道如何只显示用户仪表板中的帖子(用户仪表板可以从任何用户访问,所以,current_user不会工作!)

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

  #.....
end

This will display only the posts of that particular user only. 这将仅显示该特定用户的帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM