提问人:pinkfloyd90 提问时间:3/31/2022 更新时间:4/1/2022 访问量:86
您可以使用 JS 刷新嵌套对象,以便重新加载依赖于父属性的属性吗?
Can you refresh a nested object with JS so the attributes that depend on parent attributes are reloaded as well?
问:
我有一个模型。我正在将表单嵌套在表单中。Plan
has_many
Version
Version
Plan
形式
<%= simple_form_for @plan do |f| %>
<%= f.simple_fields_for :version do |v| %>
<%= v.input :amount, hint: "#{pricing_model_hint_for_amount(@version)}" %>
<% end %>
<% end %>
我在 PlansController #new 操作中构建 Version:
控制器操作
def new
@plan = current_account.plans.build
@version = @plan.versions.build
end
例如,输入的提示取决于其父级的属性,称为 。输入的 hint 属性使用一个帮助程序,该帮助程序将查看 以从 YAML 文件中获取正确的提示。@version
amount
Plan
:pricing_model
@version.plan.pricing_model
助手
def pricing_model_hint_for_amount(version)
t("simple_form.hints.version.pricing_model.#{version.plan.pricing_model}")
end
问题:
因为在加载表单时被实例化,所以当我在表单中更改提示时,提示不会动态更新。@version
@plan
pricing_model
有没有办法(有或没有 Javascript)重新加载嵌套的,以便在其父级中的某些内容发生变化时相应地更新表单提示?如果没有,有没有办法重新加载提示字段本身,以便在再次使用帮助程序时获得相同的结果?@plan
我希望我解释了自己,否则请告诉我。谢谢。
答:
评论