开发者

edit input value before saving in Rails

开发者 https://www.devze.com 2023-03-01 21:30 出处:网络
Hi I have a very simple input field for a URL from a user: <%= f.text_field :url, :value=>\"#{@url_website开发者_高级运维}\" %>

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!


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


Putting

before_save :adjust_url

in the model does the trick.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号