rails set selection value to null rather than blank - ruby-on-rails

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.

Related

Getting Globalize and autocomplete to work together

Using globalize gem to manage translations with autocomplete, there is a situation where a number of hooks need to be properly set. Note: this does not use hstore AFAIK. I have not managed to find a way to do so. The most productive set-up to date has
controller:
autocomplete :nation, :name, :full => true
Nation
translates :name
view
<%= autocomplete_field_tag 'nation_name', '', autocomplete_nation_name_itins_path, size: 35, :id_element => 'nation_id' %>
There is no inherent reference to nation_translations database table created by Globalize as of yet. As this image suggests, there is a problem:
Issue 1: The input remains binded to the base table's attribute value (I have not yet cleared them out as the Globalize gem suggests. Otherwise I'd be getting blanks). can is actually ready all values of canin master table... Typing in other locales, like cyrillic say Канада has naturally no effect as that value is not part of the Nation table.
What is interesting is that the drop-down values are being populated by Rails automatically, extracting the translation values of what is input.
Issue 2: I'd rather pass the parameter 'nation_id' which is naturally part of the nation_translations table with the form data. although I can append , :extra_data => [:nation_id] to the controller it is not being submitted (example in cyrillic where the input is given without any autocomplete)
{"utf8"=>"✓", "nation_name"=>"Канада", "commit"=>"..."}
Rails.logger.info :extra_data returns:
extra_data
Now the second issue can be overcome because a query like
Nation::Translation.where('name = ?', "Канада").pluck('nation_id')
returns a proper result. But that point is moot if the autocomplete is not playing ball with the user's input.
How can this be configured to have user input autocomplete with the current local translations?
this does get solved by creating an entirely new class with attributes nation_id, name, locale and can thus be called symbolically.
The query call is not that straightforward however. As the gem suggests, the method need to be tweaked
def autocomplete_nation_name
term = params[:term].downcase
locale = params[:locale]
nationtranslations = Nationtranslation.where('locale = ? AND name LIKE ?', locale, "%#{term}%").order(:name).all
render :json => nationtranslations.map { |nationtranslation| {:id => nationtranslation.id, :label => nationtranslation.name, :value => nationetranslation.name} }
end
intervening on the action method itself provides all the leeway desired...

'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.

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.

How do I display the current value in an edit form using haml custom inputs?

The code I'm working with is dynamically rendering the input fields in this edit form, like so:
%input.custom{:name => "short_form[#{field_name}]", :type => field_type, :id => field_id, :value => short_form.send(field_name)}
Based on the field_type value it renders a certain type of input field. However, I cannot get the value to display at all. The value attribute just ends up showing up as blank. Any suggestions? field_name is the attribute name btw, and short_form is the object (this is in a partial).
Do:
:value => short_form.object.send(field_name)

drop down list option value to null

I have a drop down list with multiple radio button values to choose from. If you don' t choose a option the value will be saved in the database as null. But if you choose an option, you can't uncheck it. So I need a "not stated" option in the dropdown list instead, which would save the value as null in the database.
How can I in the best possible way change these settings so that I can save the value null in the database. I need to alter this code below so an extra option "not selected" value appears and when "not selected" is chosen the value null will be saved to the database
<%= account_pref.input "editorial_#{key}".to_sym, :as => :radio, :collection => options_for(Editorial, key.to_sym), :wrapper_html => { :class => "compact" }, :label => key.titleize %>
Just the empty string should do it I think.
:collection => [['<none>','']] + options_for(Editorial, key.to_sym)
That worked for me with an integer field, for a string field it might be harder because option values can only be strings and there's not really a concept of null values in forms. Maybe someone knows a clever trick here but you could always sort it out in the controller:
def create
#account = Account.new(params[:account])
#account.editorial = nil if #account.editorial.empty?
...
end
Or maybe override the setter in your model to do the same thing. Either option feels a bit hack-ish, though.

Resources