drop down list option value to null - ruby-on-rails

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.

Related

how to have blank value in options_for_select if no recorded value, show chosen value if record exists?

This is sort of a silly questions since I do have a way of solving this but I am wondering ig there is a better way to doing this. I notice rails has many magical ways to doing thangs that "just worked"..... So I wonder if there is a better, rails way, to do thiss
In an options_for_select form helper, I would like to have a value chosen if there is a value already exists in the database, else, blank.
The following has a blank value no matter if something exists or not:
<%= ff.select :image_file_id, options_for_select(#image_files.map { |image| [image.id, {'data-img-src'=>image.image_file_url(:thumb)}]}), {:include_blank => 'Choose None'}, class: "image-picker" %>
This has a default value of whats exists in the database, but if nothing exists in the database, it shows the first value in the databases:
<%= ff.select :image_file_id, options_for_select(#image_files.map { |image| [image.id, {'data-img-src'=>image.image_file_url(:thumb)}]}, ff.object.image_file_id), {:include_blank => 'Choose None'}, class: "image-picker" %>
I read prompt would do what I want, showing blank if record not exists, shows value if value exists, but if didn't seem to work any way I tried.
Aside from doing and if else statement, if there a way to do this in one rails magic line?
include_blank: #image_files.empty? ? 'Choose None' : nil
In fact it's also if else

Stored value not being set to nil when user deselects all options - rails / simple_form

I have the following form input which is working, except for one small 'bug'
<%= f.input :secondary_role, :collection => UseridRole::VALUES, :include_blank => false,:label => "Secondary Role(s):", :input_html => { :class => " simple_form_bgcolour simple_form_position overide_selection_field_width", :size => 4, multiple: true }, include_hidden: false, :hint => "Hold shift to select multiple roles" %>
My user's secondary role is stored in my DB as an array:-
"secondary_role": ["moderator"]
My problem is that in cases where the user has set a secondary role, that can't be un-set by deselecting the option. Nothing happens when an option is deselected in the list and saved. The above moderator can only be removed by selecting another option. How can I make the array empty upon deselection of all?
Remove the include_hidden: false option; the hidden field is there to make this work automatically.
(Otherwise, you'll need to handle the situation manually in the controller action that receives the form submission.)
So as #matthewd pointed out, the solution was to remove the include_hidden: false option, although this introduced another issue of a blank array item being added to my roles array.
So, I added the below method to trigger before every save and remove any blank array entries:
def remove_blank_entry
secondary_role = self.secondary_role
if secondary_role.include? ""
secondary_role.delete("")
end
end

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

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)

Resources