How to set simple_form input default value

怎甘沉沦 提交于 2021-02-18 19:16:12

问题


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

How can I set default value for this?


回答1:


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



回答2:


use this code:

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



回答3:


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.




回答4:


try this:

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



回答5:


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



来源:https://stackoverflow.com/questions/31309664/how-to-set-simple-form-input-default-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!