simple_form 如果为空则默认集合
simple_form default collection if blank
在我的简单形式中,我想引用一个集合,并具有以下行为:
- 如果字段为空,则使用集合中指定的默认值
- 允许用户下拉一组有效值
- 如果记录已经被持久化,则默认显示保存的值
到目前为止我的代码是这样的(也使用 simple_enum)
<%= f.input :back_language,
collection: enum_option_pairs(Flashcard, :back_language),
label: false,
selected: current_user.fluent_language %>
如何设置不覆盖已保存值的默认值?
您可以尝试只设置 current_user.fluent_language
的值,如果它不存在而不保存它:
<% current_user.fluent_language ||= "you_default_value" %>
<%= f.input :back_language,
collection: enum_option_pairs(Flashcard, :back_language),
label: false,
selected: current_user.fluent_language %>
编辑:
我不确定你对 fluent_language
和 back_language
的实现是什么样的,但根据评论,一个可能更好的解决方案是这样的:
<% current_user.back_language ||= "you_default_value" %>
<%= f.input :back_language,
collection: enum_option_pairs(Flashcard, :back_language),
label: false %>
编辑:
事实证明,改革有一种简单的方法来实现这一点。 :)
在表单对象中调用property
时,只需传入default
选项即可。
property :back_language, default: model.fluent_language
瞧!
原始答案:
将 simple_form
与 reform
一起使用时,表单对象在您调用 property
时定义 getters 和设置器。这些方法被委托给 model
,但如果您需要以不同方式填充表单或在保存前操作表单数据,您可以覆盖它们。
在您的情况下,您可能希望覆盖 back_language
的 getter 方法。
在您的模板中,您将只有:
<%= f.input :back_language,
collection: enum_option_pairs(Flashcard, :back_language),
label: false %>
表单对象将具有:
model :user
property :back_language
def back_language
# takes care of both new and edit actions
super || model.fluent_language
end
交替
如果你不想处理表单对象,你可以这样做:
<%= f.input :back_language,
collection: enum_option_pairs(Flashcard, :back_language),
label: false,
selected: (@form.back_language || current_user.fluent_language) %>
但我不建议在模板中保留逻辑。
在我的简单形式中,我想引用一个集合,并具有以下行为:
- 如果字段为空,则使用集合中指定的默认值
- 允许用户下拉一组有效值
- 如果记录已经被持久化,则默认显示保存的值
到目前为止我的代码是这样的(也使用 simple_enum)
<%= f.input :back_language,
collection: enum_option_pairs(Flashcard, :back_language),
label: false,
selected: current_user.fluent_language %>
如何设置不覆盖已保存值的默认值?
您可以尝试只设置 current_user.fluent_language
的值,如果它不存在而不保存它:
<% current_user.fluent_language ||= "you_default_value" %>
<%= f.input :back_language,
collection: enum_option_pairs(Flashcard, :back_language),
label: false,
selected: current_user.fluent_language %>
编辑:
我不确定你对 fluent_language
和 back_language
的实现是什么样的,但根据评论,一个可能更好的解决方案是这样的:
<% current_user.back_language ||= "you_default_value" %>
<%= f.input :back_language,
collection: enum_option_pairs(Flashcard, :back_language),
label: false %>
编辑:
事实证明,改革有一种简单的方法来实现这一点。 :)
在表单对象中调用property
时,只需传入default
选项即可。
property :back_language, default: model.fluent_language
瞧!
原始答案:
将 simple_form
与 reform
一起使用时,表单对象在您调用 property
时定义 getters 和设置器。这些方法被委托给 model
,但如果您需要以不同方式填充表单或在保存前操作表单数据,您可以覆盖它们。
在您的情况下,您可能希望覆盖 back_language
的 getter 方法。
在您的模板中,您将只有:
<%= f.input :back_language,
collection: enum_option_pairs(Flashcard, :back_language),
label: false %>
表单对象将具有:
model :user
property :back_language
def back_language
# takes care of both new and edit actions
super || model.fluent_language
end
交替
如果你不想处理表单对象,你可以这样做:
<%= f.input :back_language,
collection: enum_option_pairs(Flashcard, :back_language),
label: false,
selected: (@form.back_language || current_user.fluent_language) %>
但我不建议在模板中保留逻辑。