Error Messages should Match Labels - ruby-on-rails

I have a form. Many of the labels need to have a different name than they are given in the model.
attr_accessible :pin
Simpleform view:
= f.input :pin, :label => "Secret Code"
When validation fails, the error uses the model's name for the field ("Pin"), rather than the label I set in the view ("Secret Code"). The user will be confused on what field has the error.
Currently I have a helper that changes the label:
def fix_pin_errors(msg)
msg.gsub!('Pin', 'Secret Code')
end
If I am doing this for many fields, however, it becomes tedious and not DRY.
What is the best way to have errors match labels that differ from the names the model gives them?

I18n is your friend. Read this guide. It will explain how to globally convert these so they get picked up everywhere you need.
For your example, it might look something like this:
In en.yml:
en:
activerecord:
attributes:
your_model_name:
pin: 'Secret Code'

Related

'Must contain specific value' on ActiveAdmin field

I want to test my models on ActiveAdmin by creating them manually. On one of my model, a field says 'Must contain specific value' whatever i write in it. This attribute can only contain a value from a list which is predefined. I suppose this field should not be a text field but a combobox filed with the values of this list. I looked for it on Google but can't find the answer.what I have tried upto is bellow
f.input :modele, :as => :select, :collection => proc{current_route.modele.all}
but it doesn't work and worse I don't understand what I'm doing.

Editing simpleform locales to enable custom error messages

I have a form within my app that I build using simple_form. What I am trying to do is edit my error messages to something a little different than the default. I am working on my simple_form.en.yml file and I think that may be where my problems are happening.
With what I have, i'm not sure I understand what exactly goes in the settings file, and I am hoping someone can go over what I have and advise me on where to go.
My model is like so (this is my entire model)
class FormSubmission < ActiveRecord::Base
after_create :email_sales
validates :first_name, :last_name, :organization, :email, :phone, :recognition, :inquiry, presence: true
private
def email_sales
FormSubmissionMailer.update_sales(self).deliver_now
end
end
Here is one of the areas of my view
= simple_form_for #form_submission do |f|
.fieldSet.span8
.field.reco
= f.input :first_name, input_html: { class: "formStyling" }, label: "First name", required: false
Finally, in my simple_form.en.yml file I have this here
en:
activerecord:
errors:
models:
formsubmission:
attributes:
email:
blank: "cannot be empty"
Simple_form does nothing special for validation error messages I18n and leaves all work to the default Rails I18n processing. The simple_form.en.yml localization file only deals with the various options to display the form and its elements (labels, hints, etc., see the docs) and has nothing to do with error messages.
So, if you need to set the error messages localization, have a look at the official Rails guide on I18n instead. Actually, I think that your simple_form.en.yml example might work, if you moved the error message localizations to the default Rails locale file for English: config/locales/en.yml.

Rails 4 Customize ActiveAdmin batch action collection form labels

I am using on Rails 4 and ActiveAdmin, a batch action collection form to capture input from the user as they perform a batch action ( in this case a batch mailer). Is there a way to customize the labels for the inputs?
Say for example:
batch_action :email, priority: 1 , form: {main_subject: :text,
message: :textarea
} do |ids, inputs|
batch_action_collection.find(ids).each do |user|
ContactBatchMailer.contact_batch_email(main_subject, message,...
Instead of having "main_subject", I would like to display a better formatted text, like "Main Subject", or even better, something more descriptive than the variable name by itself.
I dug in the documentation https://github.com/activeadmin/activeadmin/blob/master/docs/9-batch-actions.md#batch-action-forms but I was not able to.
Any suggestions will be much appreciated.
The form is rendered by modal_dialog.js.coffee, it should be possible to override and customize, for example see this declined pull request from #mmustala
You don't have to use a symbol for the form input name. A string will also work. So in your case you could do:
batch_action :email, priority: 1 , form: {'Main Subject': :text,
message: :textarea
} do |ids, inputs|
main_s = inputs['Main Subject']
...
end

I want to use labels for my Rails 4.0 application such that they appear on the form as well as in activerecord error messages

I have checked several posts here and none of them seemed to work for me. I don't know what I am missing.
The ruby documentation here http://apidock.com/rails/ActionView/Helpers/FormHelper/label, seems to have the answer, but it is not clear how to interpret some text. For eg.,
helpers:
label:
post:
body: "Write your entire text here"
Is this the text one should put in the en.yml file as-is (replacing the post with my own model name and body with my own field name etc?) I tried that and it didn't work. Then I tried to put this text in the app/helpers/"modelname"_helper.rb file. Didn't work there either.
helpers:
label:
representative:
fname: "First Name"
After these edits, in my view I have the code as follows:
<%= f.label :fieldname %>
In my case:
<%= f.label :fname %>
At this point when I run my app, I am expecting my custom label that to be shown on the form. It doesn't. It just shows Fname
When I see similar posts on stackoverflow even there I see the same convention being used. Looks like I have two issues going on. 1) Understanding this convention and how to interpret it and 2) The solution to my actual issue itself.
I know I can use <%= f.label :fname, 'First Name' %>, but because I am validating for fname in the model for presence, the error message says "Fname is required". I would like it to say "First Name" is required.
How else can I do this?
Please help.
If you want to translate the names of your model attributes, put something like the following in config/i18n/en.yml:
activerecord:
attribute:
representative:
fname: "First name"
body: "Write something here"
Error messages for specific attributes and specific errors can be overriden too:
activerecord:
errors:
models:
representative:
attributes:
body:
blank: "Body cannot be blank"
But I think your best option is to create a new translatable for the custom body label. In your form:
= f.label :body, t(:write_text)
In your en.yml:
write_text: "Write something here"

rails bringing back all checkboxes instead of only selected one

I am using simple_form and have a following sample tag:
<%= f.input :medical_conditions, :label=>false, :collection => medical_conditons, :as => :check_boxes%>
The collection holds about 100 checkboxes. However, when user only selects 1 or 2, everything is still getting saved to the database like this:
---
- ""
- ""
- ""
medical_conditions is a simple array in my application_helper
def medical_conditons
t = [
"Allergies/Hay Fever",
"Diabetes",
"Heart Surgery"]
return t
end
the medical_conditions field is a :string field.
What do I need to do so that only values that are selected are saved in comma separated manner.
It is not simple_form behavior. It is from Rails. See this: http://d.pr/6O2S
Try something like this in your controller (guessing at how you wrote your create/update methods)...
params[:medical_conditions].delete('') #this will remove the empty strings
#instance.update_attribute(:medical_conditions, params[:medical_conditions].join(','))
#or however you want to save the input, but the key is the .join(',') which will
#create a comma-separated string of only the selected values, which is exactly
#what you're looking for me thinks :-)
If that does the trick for you, I'd consider making a private helper method that formats the params for you so that you can use it in #create, #update, or wherever else you need it. This should keep things a little cleaner and more "rails way-ish" in your crud actions.

Resources