Why I get "zero" is not an ActiveModel compatible object. It should implement: to_partial_path error?

Why am I getting the following error?

nil not an ActiveModel compatible object. It should implement: to_partial_path.

I think the error may relate to the tutorial that I am following is using Rails 3.2 while I am 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 an error that occurs in 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) %> 
+7
polymorphism ruby-on-rails renderpartial
source share
6 answers

The problem you are facing is that you have existing records in your database that do not have content associated with them. This is because you have switched from a non-polymorphic installation to a polymorphic installation. What you need to do is look for screams that lack content_type and content_id and delete them from the database. Once they are removed, it would be useful to add

validates_associated: content

for your Shout model to ensure that data in the future does not end with "corrupting" your database.

+4
source share

Including ActiveModel in Simple Ruby Objects

The Intermediate Thoughtbot tutorial in Rails 4 has few complications, but one problem is adding ActiveModel functionality to a simple Ruby object. I see this on the Week 3 video in half an hour, and the teacher (AKA Mr Halogenandtoast) retrieves the timeline object.

Instead: add ActiveModel :: Naming that you want to include in ActiveModel :: Model - change Rails 4 to make working with regular objects easier.

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

For Thoughtbot, check out the followers for a discussion link there. Strong options were another issue for this great tutorial.

+5
source share

@shouts = current_user.shouts in this line your @shouts set to nil

check current_user.shouts , it should be returned as nil

Edit :
try it instead

<%= render @shouts.content %>

+1
source share

I found this error in my development log, which was the problem. I was a little embarrassed.

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

It seems that the fix is ​​intended only to run brew update (optional) and brew install imagemagick , for everyone who is looking for a fix in the tutorial for thought.

+1
source share

Installing imagemagick will solve the problem.

Check the requirements section in the Readme

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

0
source share

brew install imagemagick did the trick.

I had a similar error, even after clearing the database of any old records. PhotoShout was stored in the database with content_id: nil , which clearly became the source of the problem.

I cleaned up the database again, ran brew install imagemagick , and the photos started loading successfully.

0
source share

All Articles