Reputation: 776
I have the following code in my rails app:
<%= select :object,
:id,
options_for_select(Hash[@object_list.map { |object| [object.name, object.id] }]),
{:include_blank => 'Please select...'},
{} %>
If no option is selected then in my controller I receive an empty string.
How can I make the options for select send 'nil' value instead?
Upvotes: 3
Views: 4836
Reputation: 4960
I think you can achieve that by inserting nil
in your list.
<%= select :object,
:id,
options_for_select(Hash[@object_list.map { |object| [object.name, object.id] }].merge({:0 => nil}), selected: 0) %>
Upvotes: 1
Reputation: 3869
You should use before_action
callback for your controller.
class SomeController
before_action :prepare_params
private
def prepare_params
params[:your_param] = nil if params[:your_param].blank?
end
end
Upvotes: 1