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

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

Thanks!

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