Rails + CarrierWave: NoMethodError: undefined method `name 'for nil: NilClass

I am using CarrierWave with Rails 3.1. When submitting a form (attempt to upload an image), the following error message appears:

Error message:

ActiveRecord::StatementInvalid in Admin::PostsController#create NoMethodError: undefined method `name' for nil:NilClass: INSERT INTO "posts" ("body", "created_at", "draft", "image", "post_type", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?) Rails.root: /Users/aziz/Sandbox/ruby/rails/Tumblelog Application Trace | Framework Trace | Full Trace app/controllers/admin/posts_controller.rb:18:in `create' Request Parameters: {"utf8"=>"βœ“", "authenticity_token"=>"za+zNRDGNCcujnCmO726cWCo2ze1rgaXv5bL17JGaek=", "post"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x000001014aeff0 @original_filename="AzizLight.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[image]\"; filename=\"AzizLight.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/ky/2ddtbt0d7k1g__2ctr8njcfc0000gn/T/RackMultipart20110918-21704-hp2ajt>>, "draft"=>"0", "user_id"=>"2", "post_type"=>"image"}, "commit"=>"Post"} 

The problem is that I don’t know where this name comes from, and I don’t know which variable is nil , so I can’t debug correctly (I tried to debug the log before asking here). Line 18 corresponds to the line @post.save in the following controller:

PostsController:

 # ... def new @post = Post.new @form_html_options = (params[:post_type] == "image") ? { :multipart => true } : {} @form_partial = get_form_partial(params[:post_type]) redirect_to admin_posts_path, :alert => "You tried to create an unknown type of post..." if @form_partial.nil? @title = "Creating a new post..." end def create @post = Post.new(params[:post]) if @post.save flash[:success] = "Post created successfully!" redirect_to admin_post_path(@post) else @title = "Creating a new post..." @form_partial = get_form_partial(params[:post][:post_type]) render 'new' end end # ... 

There may be other files that may be needed to identify the problem:

Message (model):

 attr_accessible :title, :body, :user_id, :draft, :post_type, :image belongs_to :user mount_uploader :image_url, ImageUploader 

ImageUploader:

 class ImageUploader < CarrierWave::Uploader::Base include CarrierWave::RMagick storage :fog def extension_white_list %w(jpg jpeg gif png) end end 

new.html.erb:

 <h1><%= @title %></h1> <%= form_for @post, :url => admin_posts_path, :html => @form_html_options do |f| %> <%= render 'form', :f => f %> <% end %> 

_form.html.erb:

 <%= render 'error_messages' %> <%= render @form_partial, :f => f %> <p class="drop-down"> <%= f.label :draft, 'Status' %> <%= f.select(:draft, options_for_select([["Published", 0], ["Draft", 1]], (@post.new_record? ? 0: @post.draft))) %> </p> <%= f.hidden_field :user_id, :value => @post.user_id || current_user.id %> <%= f.hidden_field :post_type, :value => @post.post_type || params[:post_type] %> <p class="button"><%= f.submit "Post", :disable_with => 'Posting...' %></p> 

_image_form.html.erb ( @form_partial ):

 <p><%= f.file_field :image %></p> 

So what is this happening?

+7
source share
6 answers

The upload image class was not loaded into the current rails server stream. Restart the rails server and it should work fine =).

+4
source

Make sure you use - mount_uploader: image, ImageUploader in your model, as here -

 class CarImage < ActiveRecord::Base belongs_to :car mount_uploader :image, ImageUploader end 

Hi

Robbie

+2
source

I had some kind of problem, and the reason (probably always) is because the UploadedFile object was sent to the carrierwave attribute. The db adapter cannot serialize this object and therefore will throw this error.

Make sure that:

  • The bootloader is installed correctly.
  • you are not using write_attribute to write the downloaded file (which caused my problem). Instead, use the accessory: model.send('image=', params[:model][:image]) . Awful, but better.
+2
source

Make sure in your model:

 mount_uploader :image, ImageLoader 

Remember that: the image must be a string / text type.

+2
source

I came across this message because I had the same error that you described, but restarting the server did not resolve it (as suggested by other answers). In my case, the problem was caused by the fact that I used mount_uploader until attr_accessible . Switching them, I solved the problem.

+1
source

I had a similar problem. The solution was changing:

 attr_accessible :title, :body, :user_id, :draft, :post_type, :image belongs_to :user mount_uploader :image_url, ImageUploader 

in

 attr_accessible :title, :body, :user_id, :draft, :post_type, :image_url belongs_to :user mount_uploader :image_url, ImageUploader 
0
source

All Articles