问题
ok so I am trying to display a users microposts in two different places based on a hidden_tag_field that is a column in the database called kind. This kind is either "purchase" or "micropost".
The app/views/users/show.html.erb (where I want both lists to be displayed)
<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>
Then in app/controllers/microposts.rb
def show
@microposts= Micropost.all
@purchases = @microposts.where(:kind => "purchase")
@sales = @microposts.where(:kind => "sale")
end
The error arises at the @purchases and @sales. Finally here is the 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>
Here is the error:
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'
来源:https://stackoverflow.com/questions/12469755/ror-nil-is-not-an-activemodel-compatible-object-that-returns-a-valid-partial