开发者

Rails: multiple (context-sensitive) validation error messages

开发者 https://www.devze.com 2023-02-28 20:54 出处:网络
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

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.

Here it is: I have an existing Rails (2.3.5) app with the usual HTML interface. I've been asked to take a subset of this app and make it available for use as an API. Where I'm stumbling is in trying to p开发者_Python百科rovide 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".

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.

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. 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). This is brittle and will break the minute a product manager asks for a change in the HTML error message.

Is there a better way to accomplish this in Rails than the one I've outlined above? 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 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 (e.g. tags, etc.).

And you don't have to use XML - you could use any format (JSON, XML, or even your own custom format).

Check your "routes.rb" file, and you'll see what I mean. Routes can contain a :format symbol, which is what tells the controller what format is being requested. 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.


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.

  2. Add a "is_using_api" variable to my user model and build the error message appropriately based on the value of this variable. 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). This is brittle and will break the minute a product manager asks for a change in the HTML error message.

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. 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).
  2. Go through this link and see if it helpslink
0

精彩评论

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

关注公众号