简体   繁体   English

Rails:多个(上下文敏感)验证错误消息

[英]Rails: multiple (context-sensitive) validation error messages

First off, I'm new to Rails, so my apologies if this question has an obvious answer - I've spent a few hours searching and haven't found one yet, but perhaps I just don't know how to phrase it for Google. 首先,我是Rails的新手,如果这个问题有一个明显的答案,我深表歉意-我已经花了几个小时进行搜索,但还没有找到,但是也许我只是不知道该如何表达谷歌。

Here it is: I have an existing Rails (2.3.5) app with the usual HTML interface. 在这里:我有一个具有常规HTML界面的现有Rails(2.3.5)应用程序。 I've been asked to take a subset of this app and make it available for use as an API. 我被要求使用该应用程序的一个子集,并将其用作API。 Where I'm stumbling is in trying to provide alternate model validation error messages. 我绊脚石的地方是尝试提供替代的模型验证错误消息。 When a human user tries to register a new account and his username is too short the validation message should be "Please enter a valid username", for example, but when someone tries to register a new account via the API the same problem should trigger the message "USERNAME_TOO_SHORT". 例如,当人类用户尝试注册新帐户且其用户名太短时,验证消息应为“请输入有效的用户名”,但是当有人尝试通过API注册新帐户时,相同的问题应触发消息“ USERNAME_TOO_SHORT”。

Ideally I'd like to extend the validation framework so that I could do something like this in my user model: 理想情况下,我想扩展验证框架,以便可以在用户模型中执行以下操作:

validates_length_of       :user_name, :minimum => 6,
:message => 'Please enter a valid username',
:api_message => 'USERNAME_TOO_SHORT',

...and then choose to use the api_message in the view or controller for the API. ...然后选择在视图或API的控制器中使用api_message。

Other things I've considered include: 我考虑过的其他内容包括:

  • Add a "is_using_api" variable to my user model and build the error message appropriately based on the value of this variable. 在我的用户模型中添加一个“ is_using_api”变量,并根据该变量的值适当地构建错误消息。 I really don't want to pollute the model this way, though. 不过,我真的不想这样污染模型。
  • Review the user.errors object in the view or controller and have a mapping that translates "Please enter a valid username" -> "USERNAME_TOO_SHORT" (for example). 在视图或控制器中查看user.errors对象,并具有一个转换为“请输入有效的用户名”->“ USERNAME_TOO_SHORT”的映射(例如)。 This is brittle and will break the minute a product manager asks for a change in the HTML error message. 这很脆弱,并且会中断产品经理要求更改HTML错误消息的时间。

Is there a better way to accomplish this in Rails than the one I've outlined above? 在Rails中,有没有比上面概述的方法更好的方法? If not, does anyone have any suggestions on if/how it might be possible to implement the path I've outlined? 如果没有,那么是否有人会/是否有可能实施我概述的路径?

This can be accomplished by utilizing a "respond_to" block 这可以通过使用“ respond_to”块来完成

respond_to do |format|
  format.html {
    ... the response for HTML (i.e. browser clients) ...
  }
  format.xml {
    ... the response for API clients
  }
  format.myformat {
    ... your custom format - you can literally name it anything you want
  }
end

It's designed to customize the response depending on what format people are using to request your information. 它旨在根据人们用来请求您信息的格式来自定义响应。 Above is just one example, but typically if you're going to build an API, you won't be responding with HTML, since then the client may have to parse through a bunch of tags that would be irrelevant to them (eg tags, etc.). 以上只是一个示例,但是通常情况下,如果您要构建API,则不会使用HTML进行响应,因为那样一来,客户端可能必须解析一堆与它们无关的标签(例如,等等。)。

And you don't have to use XML - you could use any format (JSON, XML, or even your own custom format). 而且您不必使用XML-您可以使用任何格式(JSON,XML甚至您自己的自定义格式)。

Check your "routes.rb" file, and you'll see what I mean. 检查您的“ routes.rb”文件,您将明白我的意思。 Routes can contain a :format symbol, which is what tells the controller what format is being requested. 路由可以包含:format符号,该符号告诉控制器正在请求哪种格式。 That is how the controller knows which code block to execute. 控制器就是这样知道执行哪个代码块的。

Also, if you go here and do a text search for ":format" that my also help. 另外,如果您在这里搜索“:format”,这对我也有帮助。

Following are the ways you have said: 以下是您说的方式:

  1. Ideally I'd like to extend the validation framework, add :api_message => 'USERNAME_TOO_SHORT', ...and then choose to use the api_message in the view or controller for the API. 理想情况下,我想扩展验证框架,添加:api_message =>'USERNAME_TOO_SHORT',...然后选择在视图或API的控制器中使用api_message。

  2. Add a "is_using_api" variable to my user model and build the error message appropriately based on the value of this variable. 在我的用户模型中添加一个“ is_using_api”变量,并根据该变量的值适当地构建错误消息。 I really don't want to pollute the model this way, though. 不过,我真的不想这样污染模型。

  3. Review the user.errors object in the view or controller and have a mapping that translates "Please enter a valid username" -> "USERNAME_TOO_SHORT" (for example). 在视图或控制器中查看user.errors对象,并具有一个转换为“请输入有效的用户名”->“ USERNAME_TOO_SHORT”的映射(例如)。 This is brittle and will break the minute a product manager asks for a change in the HTML error message. 这很脆弱,并且会中断产品经理要求更改HTML错误消息的时间。

All of the above are not good enough or say, bad pratices for good software design. 以上所有这些都不足以说明软件设计良好。
I propose: 我提议:

  1. Make a YAML file of all the errors that you want to change in your API, and store them in the proper hierarchy. 为您要在API中更改的所有错误制作一个YAML文件,并将其存储在正确的层次结构中。 This way, if there is any change, it wont damage the whole system, and the changes required would be minimal. 这样,如果有任何更改,它不会损坏整个系统,所需的更改将降至最低。 You can call the apt error or warning message by simply calling it from the yml file(don't forget to require the file at the beginning). 您可以通过简单地从yml文件中调用apt错误或警告消息来进行调用(请不要忘记在开始时要求该文件)。
  2. Go through this link and see if it helps link 通过这个链接去看看它是否有助于链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM