67

im trying to do default value for input

works ok:

<%= f.input_field :quantity, default: '1' %> 

but i need f.input not f.input_field

<%= f.input :quantity %> 


  • im trying it with standard html value - but after unsucessfull validation quantity is overriden by 1 - undesired

    <%= f.input :quantity, input_html: {value: '1'} %>
    
  • when i remove value and validation is unsucessfull quantity is populated - everything is ok

    <%= f.input :quantity %>
    

how to solve this ? is there any alternative like in f.input_field - :default ? or there is any other solution with value ?

6 Answers 6

163

You can try with something like this:

<%= f.input :quantity, input_html: {value: f.object.quantity || '1'} %>
Sign up to request clarification or add additional context in comments.

Comments

7

You can use the selected option of simple_form:

<%= f.input :quantity, selected: f.object.quantity || '1' %>

Comments

3

try this:

= f.input : quantity, input_html: { value: (f.object.quantity.present?) ? f.object.quantity : '1' }

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
1

This is an old question...but, none of provided answers seem acceptable to me. The best way to do this is to set the value in the controllers new action.

 def new
   WizBang.new(quantity: 1)

This will assign the objects quantity key to value 1 in the new action. The edit action should rely on the object's persisted value, or a params value if validation failed and the form reloaded. The other answers will force the quantity to 1 on edit, even if the user initially saved nil (if you allow nil). Not ok. I would not allow nil, but would include a 0 option in the quantity field.

f.input :quantity, collection (0..100)

much cleaner.

Comments

0

You can do

<%= f.input :quantity, value: f.object.quantity || '1' %>

Nowadays, leaving off the input_html key.

1 Comment

That doesn't appear to be correct. Even in the latest version of the simple_form gem (2024-01-02, v5.3.0), the README specifies the value is set by adding input_html: { value: 'mystring' }. Does this work for you? I'm interested to know if you somehow configured simple_form to accept value at the top argument level.
0

Now sure how a duplicate Question's answers get referenced, but I am sharing an Answer I just left on a question I flagged as a duplicate.

Here is a summary for this question:

 # simple_form input
 f.input :quantity, input_html: {value: f.object.quantity || '1'}

can become:

 # simple_form input
 = f.input :quantity, input_html: { value: f.object.quantity_with_default }

 # Model with date_start attribute
 class Obj
   def quantity_with_default
     # based on your model, you may need this instead: try(:quantity) || '1' 
     quantity || '1' 
   end
 end

This leaves the management of the data and its defaults in the controller instead of sprinkled throughout the HTML

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.