Note that there are some explanatory texts on larger screens.

plurals
  1. PORails + CarrierWave: NoMethodError: undefined method `name' for nil:NilClass
    text
    copied!<p>I am using CarrierWave with Rails 3.1. I am getting the following error message when I submit the form (trying to upload an image):</p> <p>Error Message:</p> <pre><code>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"=&gt;"✓", "authenticity_token"=&gt;"za+zNRDGNCcujnCmO726cWCo2ze1rgaXv5bL17JGaek=", "post"=&gt;{"image"=&gt;#&lt;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=#&lt;File:/var/folders/ky/2ddtbt0d7k1g__2ctr8njcfc0000gn/T/RackMultipart20110918-21704-hp2ajt&gt;&gt;, "draft"=&gt;"0", "user_id"=&gt;"2", "post_type"=&gt;"image"}, "commit"=&gt;"Post"} </code></pre> <p><strong>The problem is that I don't know where this <code>name</code> comes from and I don't know what variable is being nil</strong>, so I can't debug properly (I tried to debug a log before asking here). Line 18 corresponds to the <code>@post.save</code> line in the following controller:</p> <p>PostsController:</p> <pre><code># ... def new @post = Post.new @form_html_options = (params[:post_type] == "image") ? { :multipart =&gt; true } : {} @form_partial = get_form_partial(params[:post_type]) redirect_to admin_posts_path, :alert =&gt; "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 # ... </code></pre> <p>Here other files that might be needed to spot the problem:</p> <p>Post (model):</p> <pre><code>attr_accessible :title, :body, :user_id, :draft, :post_type, :image belongs_to :user mount_uploader :image_url, ImageUploader </code></pre> <p>ImageUploader:</p> <pre><code>class ImageUploader &lt; CarrierWave::Uploader::Base include CarrierWave::RMagick storage :fog def extension_white_list %w(jpg jpeg gif png) end end </code></pre> <p>new.html.erb:</p> <pre><code>&lt;h1&gt;&lt;%= @title %&gt;&lt;/h1&gt; &lt;%= form_for @post, :url =&gt; admin_posts_path, :html =&gt; @form_html_options do |f| %&gt; &lt;%= render 'form', :f =&gt; f %&gt; &lt;% end %&gt; </code></pre> <p>_form.html.erb:</p> <pre><code>&lt;%= render 'error_messages' %&gt; &lt;%= render @form_partial, :f =&gt; f %&gt; &lt;p class="drop-down"&gt; &lt;%= f.label :draft, 'Status' %&gt; &lt;%= f.select(:draft, options_for_select([["Published", 0], ["Draft", 1]], (@post.new_record? ? 0: @post.draft))) %&gt; &lt;/p&gt; &lt;%= f.hidden_field :user_id, :value =&gt; @post.user_id || current_user.id %&gt; &lt;%= f.hidden_field :post_type, :value =&gt; @post.post_type || params[:post_type] %&gt; &lt;p class="button"&gt;&lt;%= f.submit "Post", :disable_with =&gt; 'Posting...' %&gt;&lt;/p&gt; </code></pre> <p>_image_form.html.erb (<code>@form_partial</code>):</p> <pre><code>&lt;p&gt;&lt;%= f.file_field :image %&gt;&lt;/p&gt; </code></pre> <p>So what it really going on please?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload