Why am I getting 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path error?

StackOverflow https://stackoverflow.com/questions/19784675

Question

Why am I getting the following error?

nil is not an ActiveModel-compatible object. It must implement :to_partial_path.

I think the error may relate to the tutorial I'm following is using Rails 3.2 while I'm using Rails 4.

Here is the model code:

class DashboardsController < ApplicationController
  def show
    @text_shout = TextShout.new
    @photo_shout = PhotoShout.new
    @shouts = current_user.shouts
  end
end

class PhotoShoutsController < ApplicationController
  def create
    content = build_content
    shout = current_user.shouts.build(content: content)
    if shout.save
      redirect_to dashboard_path
    else
      flash.alert = "Could not shout."
      redirect_to dashboard_path
    end
  end

  private
  def build_content
    PhotoShout.new(photo_shout_parameters)
  end

  def photo_shout_parameters
    params.require(:photo_shout).permit(:image)
  end
end

Here is the view code with the error occurring on the _shout.html partial

# app/view/dashboards/show.html.erb
<%= form_for @text_shout do |form| %>
   <%= form.text_field :body, placeholder: 'Shout content here' %>
   <%= form.submit 'Shout' %>
<% end %>

<%= form_for @photo_shout do |form| %>
   <%= form.file_field :image %>
   <%= form.submit 'Shout' %>
<% end %>

<%= render @shouts %>

# app/view/shouts/_shout.html.erb
<%= div_for shout do %>
  <%= link_to shout.user.username, shout.user %>
  shouted
                                 +---------------------------------+
  <%= render shout.content %> <--| ERROR "nil' is not an Active "  |
                                 | "Model-compatible object"       |
                                 +---------------------------------+
  <%= link_to time_ago_in_words(shout.created_at), shout %>
<% end %>

# app/views/photo_shouts/_photo_shout.html.erb
<%= image_tag photo_shout.image.url(:shout) %>
Was it helpful?

Solution 2

The problem you are having is because you have existing records in your database that don't have content associated to them. This happens because you went from a non-polymorphic setup to a polymorphic setup. What you need to do is look for shouts that are missing content_type and content_id and remove them from the database. Once those are removed, it could be useful to add

validates_associated :content

to your Shout model to ensure data in the future doesn't end up "corrupting" your database.

OTHER TIPS

Including ActiveModel into plain Ruby objects

The Thoughtbot Intermediate tutorial in Rails 4 has few complications, but, one problem comes in adding ActiveModel functionality into a plain Ruby object. I see this on the Week 3 Video around the half hour mark while the tutor (AKA Mr Halogenandtoast) is extracting the timeline object.

Instead of: extend ActiveModel::Naming you want to include ActiveModel::Model - Rails 4 change to make it easier to work with plain objects.

class Timeline
  include ActiveModel::Model
...
...

For Thoughtbot learn subscribers there's a link to the discussion. Strong parameters were the other problem for this otherwise excellent tutorial.

@shouts = current_user.shouts on this line your @shouts is setting as nil

check for current_user.shouts, it must be returning as nil

Edit:
instead try this

<%= render @shouts.content %>

I found this error in my development log, which was the issue all along. I was super confused for a little while.

[paperclip] An error was received while processing <Paperclip::Errors::CommandNotFoundError: Could not run the `identify` command. Please install ImageMagick.>

Looks like the fix is just to run brew update (optional) and brew install imagemagick, for anyone else looking for the fix to the thoughtbot tutorial.

Installing imagemagick will solve the issue.

Check requirements section in Readme

https://github.com/thoughtbot/paperclip#requirements https://github.com/thoughtbot/paperclip#image-processor

brew install imagemagick did the trick.

I was having a similar error, even after cleaning out the database of any old records. The PhotoShout was being saved into the database with content_id: nil, which was clearly the source of the problem.

I cleaned out the database once more, ran brew install imagemagick and the photos began uploading successfully.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top