Rails 3: Modify error message prefix for form validation - ruby-on-rails-3

Nested forms are great, but I have noticed that it can sometimes lead to error message that are oddly formatted.
To give a concrete example:
I have a form that lets someone create a new Account. Each account has one or more Users (has_many). The signup form uses the Account model for creating the form and also includes a number of fields for creating the first User (as an account must have at least one user). In other words, it is a nested form.
Because an account can have many users, the prefix of the error messages says "users" instead of "user". Also, the error messages use the relationship name (users) and the attribute name (for example, "password") to construct the error message. This results in error message such as "Users email can't be blank." instead of "Email can't be blank.".
Is there a way to customize the error message or omit "users" from the error message?

In your :message parameter of your validation, you can add a caret which will strip the default message.
:message => "^ Email can't be blank"

Related

Adding groups to scope for Auth Code Flow pt. 1

The docs say "Add “profile” and/or “groups” to get additional user information returned in the id_token". How? What format? When I do the following:
https://.onelogin.com/oidc/2/auth?client_id=&redirect_uri=&response_type=code&scope=openid,groups
I get an error saying openid is required, as if it didn't recognize it because I tacked on the ',groups' to the querystring. What do they mean by "add" in the docs?
https://developers.onelogin.com/openid-connect/api/authorization-code

odoo server error while opening a lead,odoo 9

i have used a domian rule :
['|',('user_id','=',user.id),('message_partner_ids','child_of',[user.commercial_partner_id.id])]
under "Personal lead" record rule and object is "Lead/Opportunity"
and while clicking on a lead as user i get an error:
raise ValueError("Invalid field %r in leaf %r" % (left, str(leaf)))
ValueError: Invalid field 'user_id' in leaf ""
i know user_id does not belong to mail.followers model and so i see this error. should i change my domain rule?
my prior requirement is my users to see both
-Leads that has them as salesperson
-Leads that they follow but has different salesperson
above mentioned domain rule does satisfy that requirement , it lists all the leads but opening them gives me an error.
my prior requirement is my users to see both -Leads that has them as
salesperson -Leads that they follow but has different salesperson
You do not need to add a rule, this functionality is already supported.
The owner of the lead or someone who has access to it can add them as followers on the bottom by clicking the Add Followers and they will be able to see the document.

Slack API - Don't notify user when parsing user id

In this message formatting doc: https://api.slack.com/docs/message-formatting, you can use special control sequence characters < and > to perform server-side parsing (server-side as in Slack API's server-side).
So using <#U024BE7LH> in your chat.postMessage() call will get parsed to something like #bob or whatever the username associated with that ID is, in the actual text that shows up in slack.
Unfortunately, this will cause a notification for the person you're referring to. How do I make it so that it doesn't notify the person? I've tried to enclose in a code block, i.e.:
`<#U024BE7LH>`
or
```
<#U024BE7LH>
```
But it still pings. I'm thinking the only way is to get a list of users and parse the name from the ID.
According to this, backticks should work but empirically it hasn't for me. The Slack employee says to just convert the user ID to their name and use that without the templating.
https://forums.slackcommunity.com/s/question/0D73a000005n0OXCAY/detail?language=en_US&fromEmail=1&s1oid=00Dj0000001q028&s1nid=0DB3a000000fxl3&s1uid=0053a00000Ry9cX&s1ext=0&emkind=chatterCommentNotification&emtm=1667894666436&emvtk=fH.W2M01lq9W1cf31RSROPwB7LYs.och8RgbVTqoNlg%3D&t=1667931570045

Unable to keep default auth.user in field of table between logouts in web2py

In my db.py file I have defined a table called recipe with a field as Field('cook','string',default=auth.user.username) which basically creates a recipe with an uploaded image and username. It works fine but whenever I logout I get an error saying ('NoneType' object has no attribute 'username')
auth.user is None when the user is not logged in, so you cannot try to access the .username attribute in that case. To avoid the problem, just add some conditional logic:
default=auth.user.username if auth.user else None

How can I store extra data with a validation error message in Rails 3?

I'm trying to store some additional data along with the standard error message in a custom validator in Rails 3.
For example, (ignoring built-in validators) suppose I wanted to check to see if a post is a duplicate before it's saved. I might write my custom validation method like this:
class Post < ActiveRecord::Base
# prevent duplicate posts
validate do |post|
duplicates = Post.find_all_by_body(body)
errors.add_to_base("Post is a duplicate!") if duplicates.length
# something like this is desired:
# errors.add_to_base("Post is a duplicate",
# :extra => { :duplicates => duplicates })
end
end
This will let the user know there are duplicates, but along with adding the error message I would also like to store the duplicates so they can be displayed to the user. How would I store the list of duplicate posts retrieved during validation such that it is associated with the record's errors for the body field, and available to my view?
A simpler example might be length validation: If a field exceeds its maximum length, how can I store the maximum length along with an error message without simply interpolating it into the message as Rails currently does?
I have not had to do this before, but my first thought is to create a new method on the object called duplicates.
attr_accessor :duplicates
Then in your custom validate method, you can set the duplicates on the object making them available to the view when you render the errors. Notice your current code doesn't change much:
validate do |post|
duplicates = Post.find_all_by_body(body)
errors.add_to_base("Post is a duplicate!") if duplicates.size > 0
end
You would then have to intercept that error in the view manually so that you can print out all the duplicates if the "Post is a duplicate!" error is encountered.
You can pass options to the error but they are only used as substitution in i18n templates. To make a long story short, no you can't store meta-data about your error in the errors hash. If you need such a functionality you'll need to look into the ActiveModel::Errors module in Rails core.
Update:
Another solution could be that instead of pushing a string into error hash, you stuff an instance of your own class, a class which quacks like a string but would be decorated with extra methods and state and such like.