I have a situation where I’m uploading a certain kind of file with an
arbitrary name e.g. “op10.out”. I’m validating the file extension and
then
loading into a BLOB field in my database. These are small text files
containing NC machining code. I currently store the :filename,
:content_type and :data for each file in fields with those same names
I want to change the filename field from the original “op10.out” to
match
its program number which is stored in the :number field as something
like
“2007010”.
I could probably discard the :filename and :content_type values, because
the former is redundant and the latter is dependant on whatever mime
type a
.out file is assigned to (usually “application/octet-stream” which
because
its a general binary type, is not very useful to validate on, at least
for
me.)
I copied the upload example from the “Skateboard” book, (2nd Ed.) but
I’m
uncertain what parameters get passed back to the to controller from the
form.
How could I validate on the :filename value from the upload field in my
web
form but either discard the value or rename it to match the :number?
here’s some code:
form:
(the :name is created automatically and can’t be changed)
<% form_for(:program, :url => {:action => ‘update’, :id => @program},
:html
=> {:multipart => true}) do |form|%>
-- other fields --
<%= form.file_field :uploaded_file %></p>
<%= submit_tag "Submit Changes" %>
<% end %>
controller method
def update
@program = Program.find(params[:id])
if @program.update_attributes(params[:program])
flash[:notice] = ‘Program was successfully updated.’
redirect_to :action => ‘show’, :id => @program
else
render :action => ‘edit’
end
end
model
class Program < ActiveRecord::Base
belongs_to :programmer
belongs_to :edge
belongs_to :router
validates_presence_of :number, :description
validates_uniqueness_of :number
validates_format_of :filename,
:with => /.out$/,
:allow_nil => true,
:message => “-- you can only upload CNC .out
files”
def uploaded_file=(program_field)
unless program_field.size == 0
self.filename = base_part_of(program_field.original_filename)
self.content_type = program_field.content_type.chomp
self.data = program_field.read
end
end
def base_part_of(file_name)
File.basename(file_name).gsub(/[^\w._-]/, ‘’)
end
end
I’ve always dumped form variables from php like the following:
while(list($key, $value) = each($_REQUEST)) {
echo “$key ==> $value
\n”;
if(is_array($value)) {
while(list($key1, $value1) = each($value)) {
echo "   $key1 ==> $value1
\n";
if(is_array($value1)) {
while(list($key2, $value2) = each($value1)) {
echo "     $key2 ==>
$value2
\n";
}
}
}
}
}
is there a quick way of dumping form values to the browser in
Ruby/Rails?
Thanks!
Andrew Mansfield
Product Design & Development
KI
Green Bay, WI
Print On-Demand Price Lists! Accurate access, up-to-the minute pricing
documents 24/7. Web and print versions with new color addendum.
http://www.ki.com/pricelists/pricelists.asp?CMP=EMC-M6H981003554
CONFIDENTIALITY NOTICE: This e-mail, including attachments, is intended
solely for the person or entity to which it is addressed and may contain
confidential, privileged and/or proprietary information. Any review,
dissemination, distribution, copying, printing, or other use of this
e-mail by persons or entities other than the addressee or his/her
authorized agent is prohibited. If you have received this e-mail in
error, please contact the sender immediately and delete the material
from your computer.