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 -- Posted via http://www.ruby-forum.com/.
Taylor , you might be able to accomplish this with callbacks: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
def before_validation self.character = character.lstrip! self.character = character.rstrip! end This works perfectly. Thanks so much! Taylor -- Posted via http://www.ruby-forum.com/.
On Sunday, June 04, 2006, at 7:27 AM, Taylor Strait wrote:> def before_validation > self.character = character.lstrip! > self.character = character.rstrip! > end > >This works perfectly. Thanks so much! > > >Taylor > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsIn 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 -- Posted with http://DevLists.com. Sign up and save your mailbox.