Friday, 23 August 2013

File uploading type error in ruby on rails 3

File uploading type error in ruby on rails 3

i am new on ruby on rails and i am trying to upload files into my
database. my form is:
<% 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 %>
<br />
<%= f.submit "Upload", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
here is my controller:
def create
@conf = Conf.new(params.require(:conf).permit(:xml))
if @conf.save
flash[:success] = "New Configuration uploaded!"
redirect_to conf_show_path
else
flash[:error] = "There is a problem!"
render 'new'
end
end
this is my model:
# == Schema Information
#
# Table name: confs
#
# id :integer not null, primary key
# machine_brand :string(255)
# machine_model :string(255)
# control_unit_brand :string(255)
# control_unit_model :string(255)
# tool_axis_x :decimal(, )
# tool_axis_y :decimal(, )
# tool_axis_z :decimal(, )
# rotary_axis_number :integer
# linear_axis_number :integer
# turning_mode :boolean
# milling_mode :boolean
# description :text
# xml :file
# user_id :integer
# developer_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
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'
validates :user_id, presence: true
validates :developer_id, presence: true
end
at first my create like this and it was saying "There is a problem!"
def create
@conf = Conf.new(params[:conf])
if @conf.save
flash[:success] = "New Configuration uploaded!"
redirect_to conf_show_path
else
flash[:error] = "There is a problem!"
render 'new'
end
end
so i thought that i might defined xml part as text at the beginning. so i
created this migration file and run:
class ChangeTypeOfConfFile < ActiveRecord::Migration
def self.up
change_column :confs, :xml, :text
end
def self.down
change_column :confs, :xml, :file
end
end
console said ok. but "There is a problem!" was not solved. So i changed
the create method and add permit like above. but now i have this problem:
TypeError in ConfsController#create
can't convert Symbol into String
Rails.root: /home/staj/rails_projects/sample_app
Application Trace | Framework Trace | Full Trace
app/controllers/confs_controller.rb:40:in `create'
This error occurred while loading the following files:
conf
and this is the request if you want:
{"utf8"=>"&#10003;",
"authenticity_token"=>"D1Iw0Vu58irnbot7OBQxOp98CJevcuajgfFLh2v5Q2s=",
"conf"=>{"machine_brand"=>"ali",
"machine_model"=>"veli",
"control_unit_brand"=>"kýrk",
"control_unit_model"=>"dokuz",
"tool_axis_x"=>"50",
"tool_axis_y"=>"50",
"tool_axis_z"=>"50",
"rotary_axis_number"=>"4",
"linear_axis_number"=>"3",
"turning_mode"=>"t",
"milling_mode"=>"t",
"description"=>"desc",
"xml"=>#<ActionDispatch::Http::UploadedFile:0x00000004246dd8
@original_filename="deneme.xml",
@content_type="text/xml",
@headers="Content-Disposition: form-data; name=\"conf[xml]\";
filename=\"deneme.xml\"\r\nContent-Type: text/xml\r\n",
@tempfile=#<File:/tmp/RackMultipart20130823-13267-nlx5xv>>},
"commit"=>"Upload"}

No comments:

Post a Comment