# How do I alter posted data before it is saved in a controlle

**URL:** <https://www.ruby-forum.com/t/how-do-i-alter-posted-data-before-it-is-saved-in-a-controlle/109297>\
**Category:** Rails\
**Created:** [July 13, 2007, 7:48am UTC](https://www.ruby-forum.com/t/how-do-i-alter-posted-data-before-it-is-saved-in-a-controlle/109297 "2007-07-13T07:48:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![summea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@summea](https://www.ruby-forum.com/u/summea)\
**Post date:** [July 13, 2007, 7:48am UTC](https://www.ruby-forum.com/t/how-do-i-alter-posted-data-before-it-is-saved-in-a-controlle/109297/1 "2007-07-13T07:48:11Z")

</div>

Hi everyone,

I’m still new to Ruby and Ruby on Rails. I’ve worked with CakePHP for  
quite a while though, so I’m happy to see that most of what I’ve  
learned about CakePHP translates over (more or less,) in using Ruby on  
Rails.

But I’m stuck. After reading numerous tutorials I still can’t find an  
answer. My question is (like the title suggests,) “How do I alter  
posted data before it is saved in a controller?”

For example, say I have a simple blog program using Ruby on Rails. I  
understand how make a blog form (where one fills in a blog post…)  
and then post (save) the blog post. What I don’t understand how to do  
in Ruby is… how does one alter a value from a View that has been  
posted to a Controller?

Why would I want to do this? Simple; what if I want to make a form  
field all lowercase after it has been submitted by a user? Does  
anyone know of a method I need to be using to make this happen? I’ve  
been searching around the API ([http://api.rubyonrails.org/](http://api.rubyonrails.org/)) all  
afternoon today… but still can’t figure out why I can’t alter a  
posted variable. Thanks for any help.

Andy

---

<div class="post-metadata">

**Author:** ![summea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@summea](https://www.ruby-forum.com/u/summea)\
**Post date:** [July 13, 2007, 7:57am UTC](https://www.ruby-forum.com/t/how-do-i-alter-posted-data-before-it-is-saved-in-a-controlle/109297/2 "2007-07-13T07:57:12Z")

</div>

Use before\_save filter in your model.

before\_save :convert\_to\_lower

def convert\_to\_lower  
#do you stuff here  
self.title.downcase!  
end

Kates

---

<div class="post-metadata">

**Author:** ![summea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@summea](https://www.ruby-forum.com/u/summea)\
**Post date:** [July 13, 2007, 8:20am UTC](https://www.ruby-forum.com/t/how-do-i-alter-posted-data-before-it-is-saved-in-a-controlle/109297/3 "2007-07-13T08:20:14Z")

</div>

Thanks for the quick reply!

And I’m just making sure… but is this the “real” way to do it? (Not  
a hack, etc.)  
Thanks again,

Andy

---

<div class="post-metadata">

**Author:** ![summea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@summea](https://www.ruby-forum.com/u/summea)\
**Post date:** [July 13, 2007, 10:55am UTC](https://www.ruby-forum.com/t/how-do-i-alter-posted-data-before-it-is-saved-in-a-controlle/109297/4 "2007-07-13T10:55:19Z")

</div>

It is the real way.

I recommend you buy the “Agile Web D. with Rails” book to speed  
up your learning + understanding.

---

<div class="post-metadata">

**Author:** ![summea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@summea](https://www.ruby-forum.com/u/summea)\
**Post date:** [July 13, 2007, 2:12pm UTC](https://www.ruby-forum.com/t/how-do-i-alter-posted-data-before-it-is-saved-in-a-controlle/109297/5 "2007-07-13T14:12:18Z")

</div>

If the lower-casing of input from a web form is part of your business  
logic,  
then it belongs in the model, thus the below method is correct. If you  
follow the MVC design pattern strictly, you would have as little logic  
in  
your views and controllers as possible, pushing all logic back into the  
models.

This makes it easier to re-use your logic. Say for example that you  
have  
developed another method to add posts, one that requires a new  
controller.  
By pushing this logic into the model, you avoid having to duplicate the  
lower-casing of your field. Trivial I know, but it’s a good habit to  
get  
into.

Take a look at

[http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model](http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model)  
[http://www.therailsway.com/2007/6/1/railsconf-recap-skinny-controllers](http://www.therailsway.com/2007/6/1/railsconf-recap-skinny-controllers)  
[http://www.therailsway.com/2007/1/15/assetsgraphed-part-3](http://www.therailsway.com/2007/1/15/assetsgraphed-part-3)

These gentleman explain it much better than I ever could.

Good luck!

-Brian H.

---

<div class="post-metadata">

**Author:** ![summea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@summea](https://www.ruby-forum.com/u/summea)\
**Post date:** [July 13, 2007, 2:52pm UTC](https://www.ruby-forum.com/t/how-do-i-alter-posted-data-before-it-is-saved-in-a-controlle/109297/6 "2007-07-13T14:52:24Z")

</div>

if all you want to do is manipulate form or query data from the client  
(not necessarily always model data), just modify the data in the  
params hash

ex: title = params[:title].downcase

then do with it what you will.

however, if, as others have suggested, you want to manipulate the data  
as part of your model’s business logic, the best place to do that is  
in the model itself, as the previous example shows.

---

<div class="post-metadata">

**Author:** ![summea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@summea](https://www.ruby-forum.com/u/summea)\
**Post date:** [July 13, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/how-do-i-alter-posted-data-before-it-is-saved-in-a-controlle/109297/7 "2007-07-13T15:57:38Z")

</div>

Andy -

all the form variables are available as part of the params hash;  
depending on how your form is coded, either directly (eg  
params[:title]) or nested under the key for the appropriate model (eg.  
params[:post][:title]).

These are passed into your constructor when you make a new instance of  
a class, prior to saving et, eg:

@post = Post.new(params[:post)  
post.save

so, either, you can do as Chris said, to create a modified variable,  
and then pass that into your new object. Or you can modify the object  
directly:

@post = Post.new(params[:post])  
@post.title.downcase!  
post.save

or, you can as described above, use a filter in the _model_ to run  
code as an ActiveRecord callback.

The latter is correct, as mentioned, if that’s part of the behaviour  
of that object (eg: a particular field should ALWAYS be downcase). If  
it’s specific to the controller, you can probably get away with doing  
it in the controller, but it probably isn’t.

The PHP frameworks I’ve looked at tend to have slightly more bloated  
controllers than Rails; you want to be aiming for a skinny,  
logic-light controller, if possible. But sometimes this isn’t always  
possible, and if you’re having trouble making it that way, consider a  
refactoring later on down the line.
