Newbie Question about Ruby on rails---cant upload data

Hi everybody,

Ruby on Rails is my first programming lenguage…it looks to me like i’m
missing the big picture. I’m trying to upload a file into a database.
In my app. my user needs to select a file and upload it…however I only
want him to upload certain information from that file. I have written
code to parse only string tables from that file. For example, the table
could look like this:

“hello” = “hello world”
“sayHIgh” = “Saying hello to the world”

To parse it, I have done this in a regular ruby class (and it works!):

class Parser

table = { }
IO.foreach(‘Localizable.strings’) { |line|
if line =~ /^ \s* " (.?) " \s = \s* " (.*?) "/x
table[ $1 ] = $2
end
}

#puts table[“StaplingTitle”]
p table
end

I have my view app. in views:

Importing Strings

<%= start_form_tag({:action => 'create'}, :multipart => true)%>

Lenguage:
<%= select("variable", nil, @array_of_lenguages) %>

Path:
<%= file_field "description", "description"%>

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

<%= end_form_tag%>

My view asks for the path of the file.

So here is my problem:

Since I’m new with programming, it seems kind of confusing. What should
I do to accomplish a succesful load of those parsed keys-values into my
database?

What about the controller?

I know this is a newbie question, I really hope someone can help me out.

Thanks.

-Ferguson

On Jul 21, 2006, at 11:13 AM, Ricardo F. wrote:

I know this is a newbie question, I really hope someone can help me
out.

Thanks.

-Ferguson

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs