Rails:用于“标准化"来自用户的url输入的方法? [英] Rails: method for 'standardizing' url input from users?

查看:68
本文介绍了Rails:用于“标准化"来自用户的url输入的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Rails应用程序中的用户能够在其个人资料中共享指向其个人网站的链接.很简单,我有一列website和一个文本字段,他们可以填写使其生效.

I'd like Users in my Rails app to be able to share a link to their personal website in their profile. Simple enough, I have a column called website and a text field they can fill in to make it work.

但是,当我在视图中显示它时,出现了问题.如果使用link_to助手,则http://所包含的人员是否确定该URL是否有效.但是,如果我自动在http://之前加前缀,那么对于DID将该URL放在其网址中的用户,我会得到两次.

However, when I display this in a view there's a problem. If I use the link_to helper then whether or not the person included the http:// determines whether or not the url will work. But, if I automatically prepend http:// then I get it twice for users who DID put it in their url.

我确定这是一个相当普遍的问题,但我找不到能解决该问题的任何不错的博客文章或此类问题.

I'm sure this is a fairly common problem, but I haven't been able to find any good blog posts or SO questions that address it.

我的问题是:您如何处理应用程序中用户的URL输入?您将建议什么,以最方便用户的方式标准化最终存储在数据库中的URL?

My question is: How do you handle URL input from users in your apps? What would you recommend as the most user-friendly way to standardize the urls that end up being stored in the db?

谢谢!

推荐答案

如果我正确理解了您的问题,我可能会确保每当用户保存数据时,它都会使用诸如正在关注.

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

这篇关于Rails:用于“标准化"来自用户的url输入的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆