# Manipulating form inputs?

**URL:** <https://www.ruby-forum.com/t/manipulating-form-inputs/95501>\
**Category:** Rails\
**Created:** [June 4, 2006, 5:03am UTC](https://www.ruby-forum.com/t/manipulating-form-inputs/95501 "2006-06-04T05:03:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [June 4, 2006, 5:03am UTC](https://www.ruby-forum.com/t/manipulating-form-inputs/95501/1 "2006-06-04T05:03:22Z")

</div>

I have created a scaffold Admin/Radicals for doing CRUD. However, I’m  
not sure exactly where the scaffold uses the save() method. For a new  
entry, it creates form “radical” referencing method create(). The code  
for create() is as follows:

def create  
@radical = Radical.new(params[:radical])  
if @radical.save  
flash[:notice] = ‘Radical was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

This is no problem. I would, however, like to do some manual formatting  
of data entered. Yes I have some broad checks in the radicals.rb model  
using validates\_xx\_of but I would like to do some specific alterations.  
For example, I want to remove whitespaces from the ‘character’ field.  
So I appended create() with:

def create  
@radical = Radical.new(params[:radical])  
@radical.character.lstrip!  
@radical.character.rstrip!  
if @radical.save  
[snip]  
end

But alas this does not work. Either the validates\_length\_of in the  
model gets it or, when I comment that out, MySQL complains about “string  
too long.” (my second saftey net). Where can I intercept form data  
before it goes to the database?

Taylor

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [June 4, 2006, 5:46am UTC](https://www.ruby-forum.com/t/manipulating-form-inputs/95501/2 "2006-06-04T05:46:02Z")

</div>

Nate,

Great resource and I’m sure that is the key but I haven’t been able to  
get them to work. From my radical.rb model:

class Radical \< ActiveRecord::Base

# data validation

[snip]

def before\_validation\_on\_create  
self.character.lstrip!  
self.character.rstrip!  
end

end

No dice. My other validation schemes nab the data before it get  
written. Perhaps I am not referencing ‘character’ correctly?

Taylor

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [June 4, 2006, 7:12am UTC](https://www.ruby-forum.com/t/manipulating-form-inputs/95501/3 "2006-06-04T07:12:52Z")

</div>

Taylor,

actually i was stumped when looking over your code so i created the same  
situation.

the only way i could get it to work is if i re-assign the attribute like  
this:

def before\_validation\_on\_create  
self.character = character.lstrip!  
self.character = character.rstrip!  
end

i dont understand why but i found an example in the agile book i have.

i hope that works.

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [June 4, 2006, 7:27am UTC](https://www.ruby-forum.com/t/manipulating-form-inputs/95501/4 "2006-06-04T07:27:28Z")

</div>

def before\_validation  
self.character = character.lstrip!  
self.character = character.rstrip!  
end

This works perfectly. Thanks so much!

Taylor

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [June 4, 2006, 5:22am UTC](https://www.ruby-forum.com/t/manipulating-form-inputs/95501/5 "2006-06-04T05:22:27Z")

</div>

Taylor ,  
you might be able to accomplish this with callbacks:

> **[ActiveRecord::Callbacks](https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)**
>
> Active Record Callbacks Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic before or after a change in the object state.

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [June 4, 2006, 7:57am UTC](https://www.ruby-forum.com/t/manipulating-form-inputs/95501/6 "2006-06-04T07:57:10Z")

</div>

On Sunday, June 04, 2006, at 7:27 AM, Taylor S. wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

In the normal scaffolding flow, the ‘create’ action simply populates  
your form with data. You can add in stuff there to pre-fill certain  
form fields, etc…

When you submit the form, it passes control to the ‘new’ action, which  
is where you can modify stuff before saving it.

At the beginning of the ‘new’ action you will see a line like…

@item = Item.new(params[:item])

this line creates an Item object from the passed parameters.

You make changes to the @item after this and before it is saved.

\_Kevin
