1

我对 ruby​​ on rails 和 stackoverflow 都是新手,所以我希望我做对了。

我正在尝试编写一个表单以使用 rails 将文件上传到数据库中,但出现此错误:

ActiveRecord::StatementInvalid in ConfsController#create
NoMethodError: undefined method `name' for nil:NilClass: INSERT INTO "confs
("control_unit_brand", "control_unit_model", "created_at", "description", "developer_id",
"linear_axis_number", "machine_brand", "machine_model", "milling_mode",
"rotary_axis_number", "tool_axis_x", "tool_axis_y", "tool_axis_z", "turning_mode",
"updated_at", "user_id", "xml") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

这是请求,我认为它看起来不错:

{"utf8"=>"✓",
"authenticity_token"=>"2OLwcjq/92i4VyZvO5/0SuTosuiTzbNQtVgk+cxmPQY=",
"conf"=>{"machine_brand"=>"mbrand3",
"machine_model"=>"mmodel3",
"control_unit_brand"=>"cbrand3",
"control_unit_model"=>"cmodel3",
"tool_axis_x"=>"10",
"tool_axis_y"=>"10",
"tool_axis_z"=>"10",
"rotary_axis_number"=>"3",
"linear_axis_number"=>"3",
"turning_mode"=>"t",
"milling_mode"=>"t",
"description"=>"desc",
"xml"=>#<ActionDispatch::Http::UploadedFile:0x007f067099d180
@original_filename="deneme.xml",
@content_type="text/xml",
@headers="Content-Disposition: form-data; name=\"conf[xml]\"; filename=\"deneme.xm
\"\r\nContent-Type: text/xml\r\n",
@tempfile=#<File:/tmp/RackMultipart20130821-3666-1c0gsxu>>},
"commit"=>"Upload"}

这是我的整个 new.html.erb 文件:

<% provide(:title, 'New Configuration')%>
<h1> Upload new configuration </h1>

<div class="row">
  <div class="span6 offset3">

<%= form_for (@conf, :html => {:multipart => true}) do |f| %>

    <%= f.label :machine_brand %>
    <%= f.text_field :machine_brand %>

    <%= f.label :machine_model %>
    <%= f.text_field :machine_model %>

    <%= f.label :control_unit_brand %>
    <%= f.text_field :control_unit_brand %>

    <%= f.label :control_unit_model %>
    <%= f.text_field :control_unit_model %>

    <%= f.label :tool_axis_x %>
    <%= f.text_field :tool_axis_x %>

    <%= f.label :tool_axis_y %>
    <%= f.text_field :tool_axis_y %>

    <%= f.label :tool_axis_z %>
    <%= f.text_field :tool_axis_z %>

    <%= f.label :rotary_axis_number %>
    <%= f.text_field :rotary_axis_number %>

    <%= f.label :linear_axis_number %>
    <%= f.text_field :linear_axis_number %>

    <%= f.label :turning_mode %>
    <%= f.text_field :turning_mode %>

    <%= f.label :milling_mode %>
    <%= f.text_field :milling_mode %>

    <%= f.label :description %>
    <%= f.text_field :description %>

    <%= f.label :xml %>
    <%= f.file_field :xml %>

    <%= f.submit "Upload", class: "btn btn-large btn-primary" %>
<% end %>
  </div>
</div>

我认为问题在于创建功能,这里是:

def create

@conf = Conf.new(params[:conf])

  if @conf.save
    flash[:success] = "New Configuration uploaded!"
    redirect_to @conf
  else
    flash[:error] = "There is a problem!"
    render 'new'
  end
end

这是来自 routes.rb :

match '/conf_new', to:'confs#new'
match '/conf_show', to:'confs#index'
resources :confs

我尝试了很多方法,但无法弄清楚出了什么问题。我试图重新启动服务器,甚至重新启动计算机:),来自互联网的大量建议和代码,但都失败了。

编辑:我的 conf 模型:

class Conf < ActiveRecord::Base
attr_accessible :linear_axis_number, :control_unit_brand, :control_unit_model, 
:description, :developer_id, :machine_brand, :machine_model, :milling_mode, 
:rotary_axis_number, :tool_axis_x, :tool_axis_y, :tool_axis_z, :turning_mode, :user_id, 
:xml 

belongs_to :developer, :class_name => 'User', :foreign_key => 'developer_id'
belongs_to :receiver, :class_name => 'User', :foreign_key => 'user_id'
end

好的,我解决了这个问题。在创建中我将redirect_to @conf 更改为redirect_to conf_show_path 并且不再出现此错误

4

2 回答 2

1

您的表单不是多部分的,它应该是上传文件。像这样使用它:-

<%=form_for (@conf, :html => { :multipart => true }) do |f| %>
于 2013-08-21T09:29:31.867 回答
0

你可以试试attachment_fu。它支持多种方式上传。

于 2013-08-21T11:18:59.350 回答