使用 formattastic 将 ajax 结果作为选择列表

发布于 2024-08-14 03:56:12 字数 425 浏览 5 评论 0原文

是否可以使用 Ajax 并将结果作为 Formtastic 的选择列表?

示例:

semantic_form_for @user, :url => profile_path(@profile.id) do |f|
  - f.inputs do
    - f.input :province, :label => "Province", :as => :select, :collection => ["province-1", "province-2", "province-3"]
    - f.input :city, :label => "City", :as => :select
  - f.buttons do
    = f.submit "Submit"

我想让城市的选择列表根据省份动态变化。

is it possible to use Ajax and make the result as select list with Formtastic?

example:

semantic_form_for @user, :url => profile_path(@profile.id) do |f|
  - f.inputs do
    - f.input :province, :label => "Province", :as => :select, :collection => ["province-1", "province-2", "province-3"]
    - f.input :city, :label => "City", :as => :select
  - f.buttons do
    = f.submit "Submit"

I want to make city's select list change dynamically based on province.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒尘 2024-08-21 03:56:12

如果您使用的是 Protoype,您所描述的内容可以通过几行 Javascript 和 Prototype 来完成 Ajax.Updater

首先确保传递 Formtastic 参数:include_blank => true 在 f.input 的末尾:collection => [] 行,以便 Formtastic 知道渲染一个空的选择下拉列表。然后让你的控制器返回选项,并在你的 Haml 中:

:javascript
    new Ajax.Updater({ success: 'some_select_tag' }, '/foo', {
        // process/insert returned html options tags/data however you want
        insertion: 'bottom'
    });

If you're using Protoype, what you described can be done with a few lines of Javascript and the Prototype Ajax.Updater.

First make sure to pass Formtastic the argument :include_blank => true at the end of the f.input :collection => [] line, so that Formtastic knows to render an empty select drop-down. Then just make your controllers return the options, and in your Haml:

:javascript
    new Ajax.Updater({ success: 'some_select_tag' }, '/foo', {
        // process/insert returned html options tags/data however you want
        insertion: 'bottom'
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文