我希望他们出现在主页上。我想我可能遇到了麻烦,因为有一个belong_to has_many 关系。每个微博都属于_一个用户,每个用户都有_许多微博。我让他们出现在用户页面上(但这只是用户微博的列表,而不是我想要在主页上的所有人)。如果从视图中看不清楚。我想要两个单独的列表。每个人的微博列表都有“购买”和所有“销售”微博的另一个列表。Kind是微博数据库中的一列。现在什么都没有出现,也没有错误......
这是 app/views/static_pages/home.html.erb
<div id="purchases list">
<ol class="microposts">
<%= render @purchases unless @purchases.nil? %>
</ol>
</div>
<div id="sales list">
<ol class="microposts">
<%= render @sales unless @sales.nil? %>
</ol>
</div>
这是 app/views/microposts/_micropost.html.erb (我不确定这是否重要)
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
confirm: "You sure?",
title: micropost.content %>
<% end %>
</li>
我在模型文件(micropost.rb)中定义微帖子时使用范围可能很重要
scope :purchases, where(:kind => "purchase")
scope :sales, where(:kind => "sale")
这是 microposts_controller.rb
class MicropostsController < ApplicationController
before_filter :signed_in_user
before_filter :correct_user, only: :destroy
def home
@microposts=Micropost.all
@purchases=@microposts.collect{ |m| m if m.kind == "purchase"}.compact
@sales=@microposts.collect{ |m| m if m.kind == "sale"}.compact
def show
@micropost = Micropost.find(params[:id])
@microposts = @user.microposts
@purchases= @microposts.purchases
@sales = @microposts.sales
end
这是 users_controller.rb
class UsersController < ApplicationController
before_filter :signed_in_user,
only: [:index, :edit, :update, :destroy, :following, :followers]
before_filter :correct_user, only: [:edit, :update]
before_filter :admin_user, only: :destroy
def index
@users = User.paginate(page: params[:page])
end
def show
@user = User.find(params[:id])
@micropost=Micropost.new
@microposts = @user.microposts
@purchases= @microposts.purchases
@sales = @microposts.sales
end
def home
@microposts = Micropost.all
@purchases = @microposts.collect{ |m| m if m.kind == "purchase"}.compact
@sales = @microposts.collect{ |m| m if m.kind == "sale"}.compact
end