From: Alex Gutteridge Date: 2013-01-19T01:02:40+09:00 Subject: Re: Adding file directory automatically On 18.01.2013 14:28, Adam Kennedy wrote: > Yeah thanks Alex for all your help! > > After looking at your code, it worked fine. Brought up the correct > information and also enabled me to bid on an item. However, I tried > adding an if function too look something like this: > > if item == 'david' > files = {'david' => '../Ablums/david.txt'} > end > > As you can see, I've changed the location of the files and also the > value from the form is also lowercase d for david. > > The reason I am trying to do this is that I have 12 text files which > hold different information about music Albums, I need a function go > do > something like this: > > if item == form value > change files to {'david' => '../Albums/david.txt'} > else > if item == form value > change files to {'asher' => ../Albums/david.txt'} > end > > (sorry for the pseudo) > > So I need this function to go through 12 different item names and > depending on which one was submitted through the form it needs to > open a > different file. > > I keep receiving this error from my error_logs: > `initialize': can't convert nil into String (TypeError), referer: > http://localhost:10000/cgi-bin/david.cgi > > on line 14 where this is occuring, the line is File.open(files[item]) > > So basically it seems that my function is being skipped to line 14 > then > it can't find the 'files' and stops working. I know this is alot of > writing and i appreciate the help alot. If you have any idea of what > is > going I would love to know :) > > Adam K I think there is a fundamental misunderstanding of the idea behind my code. Instead of a series of hard-coded if/else statements don't you just need a single completely filled out 'files' Hash with separate files for each album? E.g: files = {'david' => '../Albums/david.txt', 'asher' => '../Albums/asher.txt', 'snoop' => '../Albums/snoop.txt', 'kennyg' => '../Albums/kenny.txt', 'theknife' => '../Albums/theknife.txt'} Then the File.open(files[item]) line will open the correct file based on the item given. -- Alex Gutteridge