← Back to Upcase

Can't get Paperclip styles to work


(Steven Miyakawa) #1

The Shout content_id is set to nil, when I use the following code for my PhotoShout model.

class PhotoShout < ActiveRecord::Base
  has_attached_file :image, styles: { shout: "200x200>" }
end
#<Shout id: 4, user_id: 1, created_at: "2013-07-03 22:03:01", updated_at: "2013-07-03 22:03:01", content_type: "PhotoShout", content_id: nil>

and I get the following error because of the nil content_id:

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

If I don’t use styles, it works fine, but the picture is huge. Any ideas on what the problem could be?

I have Image imagemagick-6.8.0-10 installed and I have added the following to config/environments/development.rb:

 Paperclip.options[:command_path] = "/usr/local/bin/"

(Damien Hogan) #2

Did you ever manage to solve this? I am having the same issue, however regardless if I remove the styles I get nil is not an activeModel… error.


(Ben Orenstein) #3

@halogenandtoast Anything jump out here to you?


(Damien Hogan) #4

I ended up starting the week again and for reasons unknown it just worked :confused:


(Joshua Steele) #5

Hi there,

I’m working through Week 2 of this workshop, and I’ve run into this same error with PhotoShouts. I’ve done some Googling on this, and also searched this forum, but so far haven’t been able to solve the issue. The only difference I can see in what I’m doing is that I am using Pow as my web server for development. I’ve tried restarting of course, but that has not helped.

When I first saw the error, I realized that I had not installed Imagemagick. I did that via HomeBrew, and also configured the development.rb as directed in the Paperclip docs. But the problem persists, same error.

All my current code is on GitHub here: https://github.com/joshukraine/intermediate-rails

This is the specific error I’m getting in the browser:

ArgumentError in Dashboards#show

Showing /Users/joshukraine/dev/projects/intermediate-rails/shouter/app/views/shouts/_shout.html.erb where line #4 raised:

'nil' is not an ActiveModel-compatible object that returns a valid partial path.
Extracted source (around line #4):

1: <%= div_for shout do %>
2:   <%= link_to shout.user.username, shout.user %>
3:   shouted
4:   <%= render shout.content %>
5:   <%= link_to "#{time_ago_in_words(shout.created_at)} ago", shout %>
6: <% end %>

Also, for what it’s worth, here’s what I’m seeing in the log:

Started GET "/dashboard" for 127.0.0.1 at 2014-05-08 12:23:02 +0300
Processing by DashboardsController#show as HTML
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 4 LIMIT 1
  Shout Load (0.1ms)  SELECT "shouts".* FROM "shouts" WHERE "shouts"."user_id" = 4 ORDER BY created_at DESC
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 4 LIMIT 1
  Rendered shouts/_shout.html.erb (28.3ms)
  Rendered dashboards/show.html.erb within layouts/application (49.3ms)
Completed 500 Internal Server Error in 146ms

ActionView::Template::Error ('nil' is not an ActiveModel-compatible object that returns a valid partial path.):
    1: <%= div_for shout do %>
    2:   <%= link_to shout.user.username, shout.user %>
    3:   shouted
    4:   <%= render shout.content %>
    5:   <%= link_to "#{time_ago_in_words(shout.created_at)} ago", shout %>
    6: <% end %>
  app/views/shouts/_shout.html.erb:4:in `block in _app_views_shouts__shout_html_erb__3727034432027626738_70139996934800'
  app/views/shouts/_shout.html.erb:1:in `_app_views_shouts__shout_html_erb__3727034432027626738_70139996934800'
  app/views/dashboards/show.html.erb:11:in `_app_views_dashboards_show_html_erb__2638964540940122497_70139997269620'

And lastly from the db:

#<Shout id: 10, user_id: 4, created_at: "2014-05-07 14:35:35", updated_at: "2014-05-07 14:35:35", content_type: "PhotoShout", content_id: nil>

Thanks in advance for any assistance!

Josh


(Chad Pytel) #6

@joshukraine Can you verify that identify and convert are in /usr/local/bin?

What does which convert in your terminal return?

Also, just to be sure, be sure to restart your server after making any changes to your environment.

Finally, the portion of your log that you provided is from when you’re trying to render the attachment. It would be better to look at the place where you’re uploading the file - that is where any errors from Paperclip/Imagemagick will be.

I hope that helps,
-Chad


(Joshua Steele) #7

Hi @cpytel,

Thank you for the response. Regarding identify and convert this is my terminal output:

~$ which convert
/usr/local/bin/convert
~$ which identify
/usr/local/bin/identify

Back when I was initially troubleshooting this, I had restarted the server several times, but to no avail. This morning, after receiving your response, I deleted all the shouts in my db (which I had also tried before), restarted the server, and tried to post a new photo shout. To my surprise, it worked this time. (Prior to doing this, I was still getting the error, even this morning.)

While I’m glad things are working now, I would like to try and understand what happened. To the best of my knowledge, I did not change anything. I noticed that @Damien_Hogan also experienced this - that is, things just started working for no apparent reason. (see post further up in this same thread) Any thoughts as to what might have been the problem?

For what it’s worth, this gist contains my complete log output from the steps I described above.

Thanks again,
Joshua


(Chad Pytel) #8

@joshukraine I notice you mentioned you were using pow. How were you restarting it? Its possible that it wasn’t actually restarting?


(Joshua Steele) #9

@cpytel - Yes I thought of that. Per the Pow manual, I was restarting the server with this command: $ touch tmp/restart.txt in the root of my project. Also of note here is that throughout the tutorial, there are many times when the server must be restarted in order for new code to work. The above approach always seemed to work in those cases.


(Chad Pytel) #10

The thing that comes to mind is that something with your environment needed to be initialized in order for imagemagick to be fully working, and that it took an environment/computer restart or something for it to be fully working?


(Joshua Steele) #11

@cpytel Yes, I agree that appears to be the case. It almost seems like if I had simply rebooted my machine after installing Imagemagick, everything would have worked. Not sure why, but the fact that the error just “went away” days later kind of points to that. (I use a MacBook Air and reboot only occasionally.)

I realize that, now that everything is working, troubleshooting this further could be a bit sticky, so if this is as far as we can/should go, that’s fine with me. Now that I do have Imagemagick installed, hopefully this won’t be an issue in the future. In any case, I’m glad I can move forward with the tutorial!

Thanks again for your time and input. Much appreciated!