Ruby on Rails 3、表单、ajax、嵌套、就地编辑、一对一。最佳实践

发布于 2024-10-17 02:35:47 字数 740 浏览 5 评论 0原文

假设我有一个包含许多字段的复杂嵌套表单。

我想以 ajax 方式逐一编辑其字段,以便每次我看到表单时 - 它都是“显示”样式(没有用于更改信息的字段),但可以切换任何特定字段或字段组使用其自己的“保存”或“更新”按钮进入“编辑”模式。

为了解决这类问题,我最终采用了两种方法:

  1. 扩展使用 Ryan Bates 复合体-表单示例
    这种方式的缺点是每个字段(或一组字段)都需要它自己的代码(即 javascript: 'insert_fields'),该代码呈现相应的 'edit' 样式表单,因此会导致页面被 javascript 淹没。

  2. 第二个 - 是通过 ajax 通过特殊控制器操作(即 get_partial)加载相应编辑部分的统一过程,该操作通过“编辑”表单“渲染:更新”给定字段的区域。
    对于给定的字段或字段组,我有“编辑”和“显示”的部分内容。当我需要将该字段切换到编辑模式时,我发送带有必要参数的请求 (link_to ...,'/.../get_partial?partial=foo',:remote => true) ajax,并且某些控制器#action 通过 javascript 渲染该部分。

我认为第二种方法更好,但我不知道如何最好地优化它。

对于这个问题还有更优雅的解决方案吗?

Assume, that I have a complex nested form with many fields.

I want to edit its fields one-by-one in ajax way, so that every time I see form - it is in 'show' style (without fields to change information), but with possibility to switch any particular field or group of fields to 'edit' mode with it's own 'save' or 'update' button.

Solving this kind of problem, I ended up with two ways:

  1. Extended use of Ryan Bates' complex-form-examples.
    The disadvantage of this way is that every field (or group of fields) requires it's own code (i.e. javascript: 'insert_fields'), which renders corresponding 'edit' style form, so it results in page is overwhelmed by javascripts.

  2. Second - is unified procedure of loading corresponding edit partials through ajax through special controller action (i.e. get_partial), which "render :do updates" given field's area by 'edit' form.
    For given field or group of fields i have partials for 'edit' and for 'show'. When i need to switch that field to edit mode i send request (link_to ...,'/.../get_partial?partial=foo',:remote => true) with necessary params by ajax, and some controller#action renders that partial by javascript.

I think that second approach is better one, but I can't figure out how optimize it the best.

Are there any more elegant solutions to this problem?

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

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

发布评论

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

评论(1

金橙橙 2024-10-24 02:35:47

如果您生成一个普通的“编辑”表单(包含所有嵌套字段等),然后让 javascript 隐藏字段并添加字段文本和旁边的编辑链接,会怎么样?因此,举例来说,假设您的表单如下所示:

= form_for @foo do |f|
  = f.text_field :name

并且您的 javascript 会对其执行此操作 (1):

= form_for @foo do |f|
  = f.text_field :name, :class => "hide"
  <the value of the field here>
  = link_to "edit", "#"

然后让您的 javascript 将单击事件添加到编辑链接,单击时会执行以下操作:

= form_for @foo do |f|
  = f.text_field :name
  = f.submit "Save"

然后您需要更多的 javascript 来使保存按钮提交表单(ajax),然后返回上面的(1)

What if you generated a normal 'edit' form (with all the nested fields, etc), and then had the javascript hide the fields and add the text of the field and an edit link next to it. So for example say your form looks like:

= form_for @foo do |f|
  = f.text_field :name

and your javascript would do this to it (1):

= form_for @foo do |f|
  = f.text_field :name, :class => "hide"
  <the value of the field here>
  = link_to "edit", "#"

then make your javascript add a click event to the edit links that, when clicked, does:

= form_for @foo do |f|
  = f.text_field :name
  = f.submit "Save"

then you'd need more javascript that makes the save button submit the form (ajax), and go back to (1) above

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文