Nothing uploads.....why? (newbie question)

Hi everybody,
I know this is an easy question but I don’t seem to see the solution.
I’m trying to have a user select a file so that my code can parse it and
populate the database with corresponding values. So far it seems like
uploads but nothing shows in my db. Can anybody give me a hand?

thanks

view:

h1>Importing Strings


<%=error_messages_for(“StringRecord”)%>
<%= start_form_tag({:action => ‘create’}, :multipart => true)%>


Lenguage:

<%= select(“variable”, nil, @array_of_lenguages) %>



Path:

<%= file_field (“StringRecord”, “” )%>


<%= submit_tag “Import”%>

<%= end_form_tag%>

model:
class StringRecord < ActiveRecord::Base

def import_strings(a_file)
table = { }
IO.foreach(’#{a_file}’) { |line|
if line =~ /^ \s* " (.?) " \s = \s* " (.*?) "/x
table[ $1 ] = $2
end
table.each do |key, value|
StringRecord.create(:key => key, :value => value)
end

}

end
end

I also used scaffold as my base to develop my app.

On Jul 31, 2006, at 2:08 PM, Mike Williams wrote:

Hi everybody,
I know this is an easy question but I don’t seem to see the solution.
I’m trying to have a user select a file so that my code can parse
it and
populate the database with corresponding values. So far it seems like
uploads but nothing shows in my db. Can anybody give me a hand?

Mike-

I answered this question fior either you or maybe someone you work

with already> I am pasting my response here again:

Hey Fergeson-

Let's see if we can get you going in the right direction here. You

Are most of the way there since you have your parser and view
already. You need a database table, a model for that table and a
controller to get data from the view and stick it in the model.

So first you need to create a migration for the db table. Lets call

it localized_strings. So from inside of your rails app run this command:

$ script/generate model LocalizedString

That will create a file in RAILS_ROOT/db/migrate/
001_create_localized_strings.rb . Put this inside of it:

class CreateLocalizedStrings < ActiveRecord::Migration
def self.up
create_table : localized_strings do |t|
t.column :key, :string
t.column :value, :string
end
end

def self.down
drop_table : localized_strings
end
end

Save that file and run rake migrate. You will now have a model called
LocalizedString to hold your data in after you parse it.

Lets make the view even simpler for now and just deal with the file
upload

Importing Strings

<%= start_form_tag({:action => 'create'}, :multipart => true)%>
 <p>
     <b>Path: </b><br />
     <%= file_field "localized_string", "data"%>
 </p>

 <p><%= submit_tag "Import"%></p>

<%= end_form_tag%>

Now you need a controller to handle the busy work.

class MyController < ActionController::Base

def create
   table = { }
   # params[: localized_string][:data] is a File or StringIO

object when its a file upload
params[: localized_string][:data].each_line do |line|
if line =~ /^ \s* " (.?) " \s = \s* " (.*?) "/x
table[ $1 ] = $2
end
end
# loop over key=>val pairs in table and create new model
records from each one
table.each do |key,val|
LocalizedString.create(:key => key, :value => val)
end
end

end

That should get you where you need to go.

Cheers-
-Ezra

Hi Mike,

Mike Williams wrote:

I’m trying to have a user select a file so that
my code can parse it and populate the
database with corresponding values. So far
it seems like uploads but nothing shows in my
db.

I’m not 100% sure on this, but I don’t think ‘.create’ actually creates
(i.e., saves) a new record. It might just create a new model object.
Again, not sure. You might want to try an explicit save on the records
and
see if that makes a difference.

hth,
Bill

Thanks Ezra,

Quick question. When I tried the command ‘$ script/generate model
LocalizedString’ my command didn’t recognize it. What i did instead I
typed ‘ruby script/generate model LocalizedStrings’ and that created a
regular model with a class heading “class LocalizedString <
ActiveRecord::Base”…can I simply replace the keyword ‘Base’ to
‘Migration’?

MIke

I found the answer to my question. I guess it’s a diff. syntax.
http://wiki.rubyonrails.org/rails/pages/UnderstandingMigrations

mIke

Bill W. wrote:

Hi Mike,

Mike Williams wrote:

I’m trying to have a user select a file so that
my code can parse it and populate the
database with corresponding values. So far
it seems like uploads but nothing shows in my
db.

I’m not 100% sure on this, but I don’t think ‘.create’ actually creates
(i.e., saves) a new record. It might just create a new model object.
Again, not sure. You might want to try an explicit save on the records
and
see if that makes a difference.

hth,
Bill

I get the error in the line that has ‘.create’ I’m not sure why.

Mike

Hey Ezra…

I tried what you told me, but I seem to be getting some odd error:
NoMethodError in UploadController#create
undefined method `create’ for
#ActiveRecord::ConnectionAdapters::MysqlAdapter:0x380d458
RAILS_ROOT: ./script/…/config/…

Application Trace | Framework Trace | Full Trace
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/migration.rb:272:in
send' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/migration.rb:272:inmethod_missing’
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/migration.rb:256:in
say_with_time' C:/InstantRails/ruby/lib/ruby/1.8/benchmark.rb:293:inmeasure’
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/migration.rb:256:in
say_with_time' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/migration.rb:270:inmethod_missing’
#{RAILS_ROOT}/app/controllers/upload_controller.rb:12:in `create’
#{

any ideas why this might be?

Mike

Hi Ezra,

do you know if underscores are allowed when we specify the name of the
columns when we create a migration?

thanks

On Aug 1, 2006, at 9:45 AM, Robert S. wrote:

MIke


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

No that is the actual model file. You need to look in your db/
migrations folder to see the migration file.

-Ezra

Hi Ezra…thanks for your help. I tried it and it works now! :slight_smile: thanks
much
Robert

On Aug 2, 2006, at 9:55 AM, Robert S. wrote:


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Yes underscores are fine.

-Ezra