# Need help reading a yaml file on upload with form

**URL:** <https://www.ruby-forum.com/t/need-help-reading-a-yaml-file-on-upload-with-form/181492>\
**Category:** Rails\
**Created:** [January 12, 2010, 7:47pm UTC](https://www.ruby-forum.com/t/need-help-reading-a-yaml-file-on-upload-with-form/181492 "2010-01-12T19:47:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 12, 2010, 7:47pm UTC](https://www.ruby-forum.com/t/need-help-reading-a-yaml-file-on-upload-with-form/181492/1 "2010-01-12T19:47:32Z")

</div>

Here is the current basic information:

# new (views/file\_uploads/new.html.erb)

\<% form\_for( @file\_upload, :html =\> { :multipart =\> true } ) do |f| %\>  
\<%= f.error\_messages -%\>

\<%= f.label :upload, 'File' -%\> \<%= f.file\_field :upload -%\>

\<%= f.submit( 'Upload' ) %\>

\<% end %\>
# controller

def new  
@file\_upload = FileUpload.new  
end

def create  
@theme\_upload = FileUpload.new(params[:upload])

respond\_to do |format|  
if FileUpload.save  
flash[:notice] = ‘Successfully Uploaded File.’  
format.html { redirect\_to(@file\_upload) }  
else  
format.html { render :action =\> “new” }  
end  
end  
end

# model

class FileUpload \< ActiveRecord::Base

validates\_presence\_of :upload  
attr\_accessor :upload

end

# yaml file being uploaded

file:  
name: default  
author: First L.  
date: 2010-01-01  
stylesheets: true  
javascripts: true  
layouts: true  
swfs: true

=================

What I would like to be able to do is eventually upload a number of file  
folders with files based off what is listed in a YAML file being  
selected and read.

For instance, if I could info = YAML.load\_file(the\_path\_to\_upload\_file)  
I would get the following:

=\> {“file”=\>{“name”=\>“default”, “date”=\>Fri, 01 Jan 2010,  
“author”=\>“First L.”, “swfs”=\>true, “javascripts”=\>true,  
“layouts”=\>true, “stylesheets”=\>true}}

and would be able to read/use the information later on.

My problem is understanding how to call/access that information using  
the template above. If I run it right now, of course what will happen  
is a record will be saved with nil information because nothing is truly  
being sent/saved properly.

However, when viewing it in the console, the following information is  
being sent via parameters:

Parameters: {“commit”=\>“Upload”,  
“authenticity\_token”=\>“4IW8zdd8WyHFBjxMJgxZbPtPp0XlJiPInxtlywfaf74=”,  
“file\_upload”=\>{“upload”=\>#\<File:C:/Users/MyUser/AppData/Local/Temp/RackMultipart.3076.2\>}}

If I open the Rackmultipart.3076.2 file it does match the exact same  
contents of the yaml file being uploaded.

Again, I’m unsure of how to go about defining a method to simply read  
the yaml file and parse its contents using an upload button.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 12, 2010, 7:50pm UTC](https://www.ruby-forum.com/t/need-help-reading-a-yaml-file-on-upload-with-form/181492/2 "2010-01-12T19:50:48Z")

</div>

For instructional use here is what I see:

1. Admin user goes to form.
2. Admin user selects browse and finds the yaml file.
3. Admin users selects upload.
4. yaml file is read and parsed.
5. model saves the information from the yaml file into a record.
6. uploading begins in another routine based off what was recorded in  
step 5.

I hope that helps…

My problem is understanding what to do between steps 3 - 5.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 12, 2010, 8:01pm UTC](https://www.ruby-forum.com/t/need-help-reading-a-yaml-file-on-upload-with-form/181492/3 "2010-01-12T20:01:51Z")

</div>

On Jan 12, 1:47Â pm, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Â
> 
> \<%= f.submit( ‘Upload’ ) %\>
> 
> # model
> 
> file:  
> What I would like to be able to do is eventually upload a number of file  
> and would be able to read/use the information later on.  
> “authenticity\_token”=\>“4IW8zdd8WyHFBjxMJgxZbPtPp0XlJiPInxtlywfaf74=”,  
> “file\_upload”=\>{“upload”=\>#\<File:C:/Users/MyUser/AppData/Local/Temp/RackMul tipart.3076.2\>}}
> 
> If I open the Rackmultipart.3076.2 file it does match the exact same  
> contents of the yaml file being uploaded.
> 
> ## Again, I’m unsure of how to go about defining a method to simply read the yaml file and parse its contents using an upload button.
> 
> Posted viahttp://www.ruby-forum.com/.

Are you looking for local\_path?

As in YAML.load\_file params[:file\_upload][:upload].local\_path  
?

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 12, 2010, 9:30pm UTC](https://www.ruby-forum.com/t/need-help-reading-a-yaml-file-on-upload-with-form/181492/4 "2010-01-12T21:30:06Z")

</div>

pharrington wrote:

> Are you looking for local\_path?
> 
> As in YAML.load\_file params[:file\_upload][:upload].local\_path  
> ?

Thanks mate - yes this is exactly what I was looking for. I have not  
done a lot of work with reading files and am becoming familiar with how  
to do certain things with them. I couldn’t figure out how to translate  
the local\_path and now you showed me.

Many thanks!
