Rails: method for 'standardizing' url input from users?

可紊 提交于 2019-12-04 12:20:06

If I understand your problem correctly I would probably make sure that whenever a user saves their data it checks and modifies (if needed) the website attribute using something like the following.

class User
  before_save :sanitize_website

  def sanitize_website
    unless self.website.include?("http://") || self.website.include?("https://")
      self.website = "http://" + self.website
    end
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!