Rails + CarrierWave:NoMethodError:nil:NilClass的未定义方法“名称” [英] Rails + CarrierWave: NoMethodError: undefined method `name' for nil:NilClass

查看:78
本文介绍了Rails + CarrierWave:NoMethodError:nil:NilClass的未定义方法“名称”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将CarrierWave与Rails 3.1一起使用。提交表单时(尝试上传图像),我收到以下错误消息:

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):

错误消息:

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"}

问题是我不知道这个名称的来源,也不知道哪个变量为零,所以我无法正确调试(我在此处询问之前尝试调试日志)。
第18行对应于以下控制器中的 @ post.save 行:

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

PostsController:

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

# ...

这里可能还需要其他文件来发现问题:

Here other files that might be needed to spot the problem:

发布(模型):

attr_accessible :title, :body, :user_id, :draft, :post_type, :image

belongs_to :user

mount_uploader :image_url, ImageUploader

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:

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:

_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 ):

_image_form.html.erb (@form_partial):

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

那么请问这到底是怎么回事?

So what it really going on please?

推荐答案

您的图片上传器类未加载到当前的Rails服务器线程中。重新加载Rails服务器,它应该可以正常工作=)。

Your image uploader class was not loaded into your current rails server thread. Reload rails server and it should work fine =).

这篇关于Rails + CarrierWave:NoMethodError:nil:NilClass的未定义方法“名称”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆