Changing an attribute value

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 " &nbsp $key1 ==> $value1
\n";
if(is_array($value1)) {
while(list($key2, $value2) = each($value1)) {
echo " &nbsp &nbsp $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.

On Apr 20, 6:16 pm, “Andrew Mansfield” [email protected]
wrote:

is there a quick way of dumping form values to the browser in Ruby/Rails?

I’m not sure of the answers to your other questions, but for this one,
have you tried:
<%= debug(params) %>

?

is there a quick way of dumping form values to the browser in Ruby/Rails?

I think the easiest way is to do a tail -f on the log file. In
development mode the params from each request are written to the log
file. Assuming the form is filing successfully there is no easy way
to display the params on a web page. The create and update actions
save the record and then redirect to another action. From the browser
you can only see the params from the redirect, not very interesting.
That said a great way to see params, the current session, and a bunch
of other stuff on any page is to install the textmate footnotes
plugin. The features I mention work even if you don’t use textmate.

http://blog.inquirylabs.com/2006/09/28/textmate-footnotes-v16-released/

Aaron

Hello Andrew,

is there a quick way of dumping form values to the browser in Ruby/
Rails?

Yup.

When running in development mode putting this in your controller
actions.

def myaction
raise params
end

-christos

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