RoR: 'nil' is not an ActiveModel-compatible object that returns a valid partial path

风流意气都作罢 提交于 2020-01-07 04:58:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!