Forum: Ruby on Rails Problem adding data to db

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Edgar G. (Guest)
on 2007-05-29 01:01
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)%>

<!--[form:producto]-->
<p><label for="product_name">Name</label><br/>
<%= text_field 'product', 'name'  %></p>

<p><label for="product_description">Description</label><br/>
<%= text_area 'product', 'description'  %></p>

<!--[eoform:producto]-->

<fieldset>
    <legend>Image</legend>
    <% fields_for "file[]" do |f|%>
    <p><label for="product_file">File: </label><br/>
       <%= f.file_field :uploaded_file  %></p>
    <%end%>
</fieldset>


<fieldset>
    <legend>Pdf</legend>
    <% fields_for "file[]" do |f|%>
    <p><label for="product_file">File: </label><br/>
       <%= f.file_field :uploaded_file  %></p>
    <%end%>
</fieldset>


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:in `load'
-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
Edgar G. (Guest)
on 2007-05-29 03:00
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
Edgar G. (Guest)
on 2007-05-29 03:09
wow LOL, sorry the relly problem was on the file i was trying to upload
:P,

i kept with the first way, is better
This topic is locked and can not be replied to.