Forum: Ruby on Rails Manipulating form inputs?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Taylor S. (Guest)
on 2006-06-04 07:03
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
Nate C. (Guest)
on 2006-06-04 07:22
Taylor ,
you might be able to accomplish this with callbacks:
http://api.rubyonrails.org/classes/ActiveRecord/Ca...
Taylor S. (Guest)
on 2006-06-04 07:46
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
Nate C. (Guest)
on 2006-06-04 09:12
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.
Taylor S. (Guest)
on 2006-06-04 09:27
  def before_validation
    self.character = character.lstrip!
    self.character = character.rstrip!
  end

This works perfectly.  Thanks so much!


Taylor
Kevin O. (Guest)
on 2006-06-04 09:57
(Received via mailing list)
On Sunday, June 04, 2006, at 7:27 AM, Taylor S. wrote:
>--
>Posted via http://www.ruby-forum.com/.
>_______________________________________________
>Rails mailing list
>removed_email_address@domain.invalid
>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
This topic is locked and can not be replied to.