# Changing an attribute value

**URL:** <https://www.ruby-forum.com/t/changing-an-attribute-value/92580>\
**Category:** Rails\
**Created:** [April 21, 2007, 12:17am UTC](https://www.ruby-forum.com/t/changing-an-attribute-value/92580 "2007-04-21T00:17:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Mansfield](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@Andrew\_Mansfield](https://www.ruby-forum.com/u/Andrew_Mansfield)\
**Post date:** [April 21, 2007, 12:17am UTC](https://www.ruby-forum.com/t/changing-an-attribute-value/92580/1 "2007-04-21T00:17:13Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![Andrew\_Mansfield](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@Andrew\_Mansfield](https://www.ruby-forum.com/u/Andrew_Mansfield)\
**Post date:** [April 21, 2007, 12:32am UTC](https://www.ruby-forum.com/t/changing-an-attribute-value/92580/2 "2007-04-21T00:32:14Z")

</div>

On Apr 20, 6:16 pm, “Andrew Mansfield” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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) %\>

?

---

<div class="post-metadata">

**Author:** ![Andrew\_Mansfield](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@Andrew\_Mansfield](https://www.ruby-forum.com/u/Andrew_Mansfield)\
**Post date:** [April 21, 2007, 12:41am UTC](https://www.ruby-forum.com/t/changing-an-attribute-value/92580/3 "2007-04-21T00:41:15Z")

</div>

> 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/](http://blog.inquirylabs.com/2006/09/28/textmate-footnotes-v16-released/)

Aaron

---

<div class="post-metadata">

**Author:** ![Andrew\_Mansfield](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@Andrew\_Mansfield](https://www.ruby-forum.com/u/Andrew_Mansfield)\
**Post date:** [April 21, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/changing-an-attribute-value/92580/4 "2007-04-21T16:02:13Z")

</div>

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
