Wednesday 21 August 2013

undefined method `name' for nil:NilClass when trying to upload file into database using ruby on rails

undefined method `name' for nil:NilClass when trying to upload file into
database using ruby on rails

i am new to both ruby on rails and stackoverflow, so i hope i am doing
this right.
i am trying to write a form to upload files into database using rails and
i am getting this error:
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?)
this is request and i think it looks well:
{"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"}
this is my form in new.html.erb file:
<%= form_for (@conf) 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 %>
<br />
<%= f.submit "Upload", class: "btn btn-large btn-primary" %>
<% end %>
i think the problem is about create function and here it is:
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
this is from routes.rb :
match '/conf_new', to:'confs#new'
match '/conf_show', to:'confs#index'
resources :confs
i have tried lots of ways and could not figure it out what is wrong. i
tried to restart server, even restart computer :), lots of advices and
codes from internet, but all failed. thanks for every response

No comments:

Post a Comment