Carrierwave Gem: After creating user with avatar, 'cannot convert nil into string'

。_饼干妹妹 提交于 2020-01-03 04:54:08

问题


I will say when I first got carrierwave up and running, everything was working totally fine aside from the size scaling of the avatar.

So now after I tried to do scaling, everything went to hell. I uninstalled the gem, did a migration to remove avatar from user, then did another migration to add.

Still the same error. Note I am doing rails g uploader Avatar, NOT image. I have not touched the avatar_uploader.rb file, aside from adding

require 'carrierwave/orm/activerecord'

on the top line.

So, the really weird thing is:

When I go into console, and do User.last

1.9.3p0 :001 > User.last User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1 => #

This is so weird, because the user database is recognizing the image. So...thoughts?

As stated this was not happening initially.

In my users show page I have

<p>
   <label>My Avatar</label>
   <%= image_tag(@user.avatar_url) if @user.avatar? %>
   <%= f.file_field :avatar %>
   <%= f.hidden_field :avatar_cache %>
</p>

In my user form I have:

<label>My Avatar</label>
    <%= f.file_field :avatar %>
    <%= f.hidden_field :avatar_cache %>

I attached the server window to show you what is going on.

Started GET "/users/17" for 127.0.0.1 at 2012-03-12 13:26:28 -0500
Processing by UsersController#show as HTML
  Parameters: {"id"=>"17"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1   
[["id", "17"]]
  Rendered users/show.html.erb within layouts/application (1.9ms)
Completed 500 Internal Server Error in 5ms

ActionView::Template::Error (can't convert nil into String):
    7: 
    8: <p>
    9:    <label>My Avatar</label>
    10:    <%= image_tag(@user.avatar_url) if @user.avatar? %>
    11:    <%= f.file_field :avatar %>
    12:    <%= f.hidden_field :avatar_cache %>
    13:  </p>
  app/views/users/show.html.erb:10:in   
`_app_views_users_show_html_erb__498619941080127768_2168209880'
  app/controllers/users_controller.rb:18:in `show'

Any help would be greatly appreciated! Thanks :)


回答1:


Open your uploader and make sure you have the following:

def root
  Rails.root.join 'public/'
end

def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

This will probably fix the problem.




回答2:


Don't know if this will help; may be missing something you're doing in controller.

You upload to :avatar, but then reference avatar_url to retrieve it.

Mine is avatar.url.to_s

<%= image_tag user.avatar.url.to_s, {:height => 30} %>


来源:https://stackoverflow.com/questions/9672709/carrierwave-gem-after-creating-user-with-avatar-cannot-convert-nil-into-strin

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