好的,所以我试图根据 hidden_tag_field 在两个不同的地方显示用户微帖子,该字段是数据库中名为 kind 的列。这种要么是“代购”,要么是“微博”。
app/views/users/show.html.erb (我希望显示两个列表)
<section>
<div id= "purchases">
<%= render 'shared/micropost_form_purchase' %>
<div class="row">
<div class="span8">
<% if @user.microposts.any? %>
<ol class="microposts">
<%= render @purchases %>
</ol>
<% end %>
</div>
</div>
</div>
<div id="sales">
<% if @user.microposts.any? %>
<ol class="microposts">
<%= render @sales %>
</ol>
<% end %>
</div>
</section>
然后在 app/controllers/microposts.rb
def show
@microposts= Micropost.all
@purchases = @microposts.where(:kind => "purchase")
@sales = @microposts.where(:kind => "sale")
end
错误出现在@purchases 和@sales。最后是 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>
这是错误:
Completed 500 Internal Server Error in 11ms
ActionView::Template::Error ('nil' is not an ActiveModel-compatible object that returns a valid partial path.):
12: <% if @user.microposts.any? %>
13: <h3>Purchases I am interested in (<%= @user.microposts.count %>)</h3>
14: <ol class="microposts">
15: <%= render @purchases %>
16: </ol>
17:
18: <% end %>
app/views/users/show.html.erb:15:in `_app_views_users_show_html_erb__315023603463182203_22059740'