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

How can I set default value for this?

2

5 Answers 5

2
= r.input :date_start, as: :string, input_html: {value: '01/01/2015', id: 'date_start'}
Sign up to request clarification or add additional context in comments.

Comments

1

use this code:

= r.input :date_start, as: :string, input_html: {id: 'date_start',value: '07/02/2015'}

1 Comment

What's the difference between this answer and gmrash's?
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.

Comments

-1

try this:

= f.input :date_start, input_html: { value: (f.object.date_start.present?) ? f.object.date_start : Time.new }

2 Comments

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

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

2 Comments

What would now() represent? The time at the migration, or the time when someone's using the form?
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.

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.