" 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path " error in micropost model
Asked Answered
A

3

11

When I attempt to view the user profile page, I get the error above.

Here's my show.html.erb code:

<% provide(:title, @user.name) %>

<div class="row">

  <aside class="span4">
    <section>
      <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>
      </h1>
    </section>
  </aside>

  <div class="span8">
    <% if @user.microposts.any? %>
      <h3>Microposts (<%= @user.microposts.count %>)</h3>
      <ol class="microposts">
        <%= render @microposts %>
      </ol>
      <%= will_paginate @microposts %>
    <% end %>
  </div>

</div>

where <%= render @microposts %> is causing the problem.

Airtoair answered 2/12, 2013 at 4:42 Comment(0)
I
8

Do you declare the variable @microposts anywhere? At a glance, looks like what you should be doing is

<%= render @user.microposts %>
Institutional answered 2/12, 2013 at 4:56 Comment(0)
J
2

I have the same issue

yes, the @microposts is declared in the controller show method as:

def show
  @user = User.find(params[:id])
  @microposts = @user.microposts.paginate(page: params[:page])
end

Update: I've found that the show action is defined twice (one of them defines @microposts). To solve this issue, I simply removed the second show action which doesn't define @microposts. I wonder how more than one person could have managed to duplicate the show action.

Jowl answered 15/1, 2014 at 6:58 Comment(0)
M
0

I ran into this issue when I was passing a nested block to a render partial call like so:

// _content_header.haml
.content-header
  %h1= title
  = yield if block_given?

// edit.html.haml
= render partial: 'shared/content_header', locals: { title: 'Editing Movie' } do
  = link_to 'Show'

Calling render without the partial and locals keyword fixed it for me:

= render 'shared/content_header', title: 'Editing Movie' do
  = link_to 'Show'
Myxomatosis answered 18/1 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.