eran
eran

Reputation: 15156

How to modify a record before saving on Ruby on Rails

Looking for a way to either:

but this gives an error of:

undefined method `before_create' for RowsController:Class

or

tried:

before_filter :check_lowcase, :only => [:new]
def check_lowcase
  if (Term.new =~ /[^a-z]+/)
    flash[:notice] = "Sorry, must use lowercase"
    redirect_to terms_path
  end
end

this seems to just be ignored....

Upvotes: 0

Views: 5982

Answers (3)

Darren Cato
Darren Cato

Reputation: 1372

before_save { |classname| classname.myfield = myfield.downcase }

Upvotes: 0

Yule
Yule

Reputation: 9774

You need to do it on your model, not your controller:

class YourModel < ActiveRecord::Base
 before_create :downcase_stuff

  private
    def downcase_stuff
      self.myfield.downcase!
     end
end

Upvotes: 9

Manuel van Rijn
Manuel van Rijn

Reputation: 10315

    before_create :lower_case_fields

    def lower_case_fields
       self.myfield.downcase!
    end

Upvotes: 0

Related Questions