自定义验证不起作用 Rails 模型
我正在尝试验证用户的 Twitter 用户名并编写了自定义验证。然而,即使它似乎在 irb 会话中工作,它也不起作用。这是我的代码
def twitter_user
begin
@t = Twitter.user :twitter
rescue Twitter::Error::NotFound => e
@t = nil
end
errors.add(:twitter, (" username does not exist")) if @t.nil?
end
我也尝试过:
def twitter_user
errors.add(:twitter, (" username does not exist")) unless begin Twitter.user :twitter
rescue Twitter::Error::NotFound => e
end
end
即使 Twitter 用户名有效,也会出现此错误。谁能解释为什么?帮助将不胜感激。
编辑:我已将其范围缩小到传递的参数:twitter,不是用户的输入。如何获取用户传递的值?
I am trying to validate a users twitter username and have written a custom validation. However it is not working even though it appears to work for in a irb session. This is the code I have
def twitter_user
begin
@t = Twitter.user :twitter
rescue Twitter::Error::NotFound => e
@t = nil
end
errors.add(:twitter, (" username does not exist")) if @t.nil?
end
I've also tried this:
def twitter_user
errors.add(:twitter, (" username does not exist")) unless begin Twitter.user :twitter
rescue Twitter::Error::NotFound => e
end
end
This error appears even when the twitter username is valid. Can anyone explain why? Help would be appreciated thanks.
EDIT: I have narrowed it down to the param getting passed :twitter, is not the input from the user. How do I get the value passed by the user?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在模型中添加验证方法
,例如:
You have to add validate method in you model
for ex: