1
= r.input :date_start do
  = r.text_field :date_start, id: 'date_start'

How can I set default value for this?

gmrash
  • 650
  • 7
  • 15
  • duplicate question. http://stackoverflow.com/questions/19029129/default-value-for-input-with-simple-form solution: <%= f.input :date_start, input_html: {value: f.object.date_start || '2016/12/12'} %> – Carlos Montalvo Dec 13 '16 at 00:15
  • Does this answer your question? [Default value for input with simple\_form](https://stackoverflow.com/questions/19029129/default-value-for-input-with-simple-form) – SMAG Mar 27 '20 at 21:07

5 Answers5

2
= r.input :date_start, as: :string, input_html: {value: '01/01/2015', id: 'date_start'}
gmrash
  • 650
  • 7
  • 15
1

use this code:

= r.input :date_start, as: :string, input_html: {id: 'date_start',value: '07/02/2015'}
Uday kumar das
  • 1,615
  • 16
  • 33
0

Use of Safe Navigation or &. explained in this Answer to another question, is an option here; especially when chained with the || operator.

Looking at the Answer from Kiry Meas

Note: I am using Time.now instead of Time.new, as I feel it is more readable, and better conveys the intent of the default value. If you want to see the a conversation or benchmarks on now vs new, I recommend this Question. There is also a good explanation for why you may want to use Time.current instead, in this Answer )

input_html: { value: (f.object.date_start.present?) ? f.object.date_start : Time.now }

Same as this Answer to a duplicate question without explanation, the above can be rewritten as:

input_html: { value: f.object&.date_start || Time.now }

This provides the existing value if present and nil if it is not. When paired with the || operator, the logic looks for the first 'truthy' response. nil is not considered 'truthy' and the || will select the default value over nil, or Time.now in this case.

In the end, I feel like this is readable and less redundant answer to setting the default value for a simple form input.

 = f.input :date_start, input_html: { value: f.object&.date_start || Time.now }

And another reason for my refactor of Kiry's Answer, is because I fell that using a ternary in this case is approaching violating the principle of Tell, Don't Ask, as presented in example 4 from: Tell, Don't Ask: refactoring examples.

Above answers the question directly, below suggests a refactor

An explanation of Gavit's answer is to set the default in a migration for when the record is saved:

# migration
class AddDefaultToDateStartOfObj < ActiveRecord::Migration[5.2]
  def change
    change_column :objs, :date_start, -> { 'CURRENT_TIMESTAMP' }
  end
end

# simple_form input
= f.input :date_start

Note: depending on your DB type the SQL might be different, but here is a good Question in regard to the nuances of this migration.

However, this does not work when the object is new and not a record in the database, yet.

I recommend the migration as well as building a method in the objects model that handles the default to the input would cleaner / more readable while following better coding principles.

 # simple_form input
 = f.input :date_start, input_html: { value: f.object.date_start_with_default }

 # Model with date_start attribute
 class Obj
   def date_start_with_default
     # based on your model, like in Kiry's comment to their own answer, you may need this instead:
     # try(:date_start) || Time.now 
     date_start || Time.now 
   end
 end

Now all of the logic for defaulting this value can be controlled / updated in a single place via the model.

SMAG
  • 652
  • 6
  • 12
-1

try this:

= f.input :date_start, input_html: { value: (f.object.date_start.present?) ? f.object.date_start : Time.new }
Kiry Meas
  • 1,200
  • 13
  • 26
  • 2
    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. – rollstuhlfahrer Mar 02 '18 at 09:44
  • i have tried this line of code: `= f.input :date_start, input_html: { value: f.object.date_start || Time.new }` but the result is empty value setting as the default value. If anyone facing the same issue as mine, the code above is working as expected. – Kiry Meas Mar 06 '18 at 03:24
-2

Specify it in the database: t.date : date_start, :null => false, :default => 'now()'

gavit
  • 509
  • 3
  • 6
  • What would `now()` represent? The time at the migration, or the time when someone's using the form? – Andrew Grimm Aug 09 '16 at 05:40
  • `NOW()` is an SQL command that gives you the current time of the server. Note: The author did not specify that they are suggesting this change in a migration instead of at the input like asked by the OP. – SMAG Mar 27 '20 at 18:39