mtay
mtay

Reputation: 1316

edit input value before saving in Rails

Hi I have a very simple input field for a URL from a user:

<%= f.text_field :url, :value=>"#{@url_website}" %>

I would like to process the input to check if they put http:// before it, and if not, add it manually. Could I run a function on it before saving? How is this done?

Thanks!

Upvotes: 1

Views: 459

Answers (2)

mtay
mtay

Reputation: 1316

Putting

before_save :adjust_url

in the model does the trick.

Upvotes: 0

Paul Kaplan
Paul Kaplan

Reputation: 2885

Well you should just put a before_save in the model to format the text, so if your model was like this

before_save :format_url

def format_url
    #put something important here
end

Upvotes: 1

Related Questions