Problem adding data to db


#1

Hello i am trying to save some data to my db, this is what i got:

MODEL:
class File < ActiveRecord::Base
belongs_to :product

def uploaded_file=(picture_field)
self.name = base_part_of(picture_field.original_filename)
self.content_type = picture_field.content_type.chomp
self.data = picture_field.read
end

def base_part_of(file_name)
File.basename(file_name).gsub(/[^\w._-]/, ‘’)
end
end

CONTROLLER:

def create
@product = Product.new(params[:product])
@product.save

for file in params[:file]
  @file = File.new(params[:file])
  @file.producto_id = @producto.id
  @file.save
end

if @product.save
  flash[:notice] = 'Product created.'
  redirect_to :action => 'list'
else
  render :action => 'new'
end

end

VIEW:
i render a partial, and tis is the partial:
<%= error_messages_for(:product)%>

Name
<%= text_field 'product', 'name' %>

Description
<%= text_area 'product', 'description' %>

Image <% fields_for "file[]" do |f|%>

File:
<%= f.file_field :uploaded_file %>

<%end%> Pdf <% fields_for "file[]" do |f|%>

File:
<%= f.file_field :uploaded_file %>

<%end%>

When i try to save i got this error:

undefined method `keys’ for #Array:0x482bbf4

RAILS_ROOT: ./script/…/config/…
Application Trace | Framework Trace | Full Trace

#{RAILS_ROOT}/app/controllers/admin/product_controller.rb:30:in create' -e:4:inload’
-e:4
Parameters: {“product”=>{“description”=>“asdfasdfasd”,
“name”=>“aasfasdf”},
“file”=>[{“uploaded_file”=>#<File:C:/Windows/Temp/CGI.5992.3>},
{“uploaded_file”=>#<File:C:/Windows/Temp/CGI.5992.4>}],
“commit”=>“Create”}
Response
Headers: {“cookie”=>[], “Cache-Control”=>“no-cache”}

helps plis :P, regards


#2

I have tried too, this solution but it get fail when try to save the
second object.

def create
@product = Product.new(params[:product])
@product.save

  @file = Archivo.new(params[:file][0])
  @file2 = Archivo.new(params[:file][1])
  @file.product_id = @product.id
  @file2.product_id = @product.id
  @file.save
  @file2.save


if @product.save
 flash[:notice] = 'Product created.'
  redirect_to :action => 'list'
else
  render :action => 'new'
end

end


#3

wow LOL, sorry the relly problem was on the file i was trying to upload
:P,

i kept with the first way, is better