1

私のレールアプリでは、ユーザーが作成した投稿をユーザーの「ダッシュボード」に表示する必要があります。

ダッシュボード コントローラ:

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 に答える