'Must contain specific value' on ActiveAdmin field - ruby-on-rails

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.

Related

ActiveAdmin: display parameter of object selected in form

I am not very familiar with ActiveAdmin or rails in general, so sorry if I use some incorrect phrasing.
I have a model for an Athlete, which has an attribute "notes". This is described in the permit_params of the athlete.rb.
On an additional page, I have the following:
f.input :athlete, :collection => Athlete.all.sort_by(&:last_name), :required => true
I would like to find a way to, if the :notes is not empty, display it.
If I could display it as a "row" on the form, that would be great.
Thanks!

rails association populate field from another table

Hoping this will be a straight forward question, but is anyone able to let me know the best way of populating a hidden field based on a value on another table.
I currently have 2 tables - table_numbers which has the following fields - id and value(decimal), and table_records which has an amount field.
On the form to add a new record I have the following to add a value to it
= f.association :table_number, :collection => table_numbers.order('value ASC'), :label_method => :value, :prompt => "Select a value", :label => "value"
At the moment this is populating the number_id on the records table, but displaying the value on the form when adding a record. What I would like is to get the value as well to be able to run a calculation on the value and amount.
What would be the best way to do this? Update the line above or do I need to add extra code?
Thanks
You can perfom calucaltions in before_save callback in model. If you want to display calculations, use helper and show it.

rails set selection value to null rather than blank

I have the following code which allows a user to select a currency rather than inheriting one.
%dd= user_field(#user, :remuneration_currency, :type => :select, :collection => REMUNERATION_CURRENCIES)
I need to allow people to set this field back to NULL, which means that the user will pick up their account's default currency. However, if I change the code to this...
%dd= user_field(#user, :remuneration_currency, :type => :select, :collection => REMUNERATION_CURRENCIES, :blank => 'Company Default')
then it sets the value to blank (as it says on the tin) rather than NULL in the database. How do I set the 'Company Default' value to be NULL rather than blank.
Your best option is to use the gem nilify_blanks which does exactly what you are looking for:
In Rails when saving a model from a form and values are not provided
by the user, an empty string is recorded to the database instead of a
NULL as many would prefer (mixing blanks and NULLs can become
confusing). This plugin allows you to specify a list of attributes (or
exceptions from all the attributes) that will be converted to nil if
they are blank before a model is saved.

Rails 4 + ActiveAdmin: Attribute limited to a few values -- customizing ActiveAdmin form based on this?

So I have a CareerEntry model that has a fullintern attribute, which is a string that is supposed to specify whether the entry represents an internship or a full-time position. I limit the values that can appear in this attribute as follows:
validates_inclusion_of :fullintern, :in => ["Internship", "Full-time"]
However, in ActiveAdmin, the part in the edit form that deals with the fullintern attribute still has a text field. How do I make it a dropdown box where the admin can select either "Internship" or "Full-time"?
Thanks.
You can use Formtastic's input helpers to use a select input:
form do |f|
f.inputs "Details" do
f.input :fullintern, as: :select, collection: ["Internship", "Full-time"]
end
f.actions
end
See Formtastic's usage section for the full set of native capabilities.

Error Messages should Match Labels

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'

Resources