Reputation: 768
I am having a products table with default min_quantity is 0 and max_quantity is MAX INT number 999999999. When I tried to display this form, the default values from backened are displayed in the UI with min_quantity as 0 and max_quantity as 999999999. How to leave this input fields as blank for the create action?
schema.rb:
create_table "products”, id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", force: :cascade do |t|
t.string "name", null: false
t.integer "min_quantity”, default: 0, null: false
t.integer "max_quantity”, default: 999999999, null: false
end
products/_form.slim:
= simple_form_for([@product]) do |f|
= f.error_notification
.form-inputs
= f.input :name
= f.input :min_quantity, label: 'Minimum Quantity'
= f.input :max_quantity, label: 'Maximum Quantity'
I tried using value : nil
as:
-if f.object.new_record?
= f.input :min_quantity, label: 'Minimum Quantity’, input_html: {value: nil}
-else
= f.input :min_quantity, label: 'Minimum Quantity'
But when there is validation error, the values entered are getting disappeared. The same issue when I use jQuery.
So how can I handle this issue of overriding blackened values in Rails? Please help
Upvotes: 0
Views: 119
Reputation: 164
You can get the behavior you want by setting the min_quantity
and max_quantity
to nil
in the new
method of the controller.
def new
@product.min_quantity = @product.max_quantity = nil
end
The reason why the values of min_quantity
and max_quantity
are show when the validation fails is because they are set in the @product
object so they actual values of the attributes are going to be shown when the form is reloaded
Upvotes: 1