以下示例取自此文档页面:
https://github.com/activescaffold/active_scaffold/wiki/Chaining-Form-Fields
[示例开始]
您可以设置列数组,以便在列更改时更新多个列,并链列更新:
class UsersController < ApplicationController
active_scaffold do |config|
config.columns[:author].form_ui = :select
config.columns[:author].update_columns = [:book, :editorial]
config.columns[:book].form_ui = :select
config.columns[:book].update_columns = :format
end
end
在此示例中,当作者更改时,书籍、编辑和格式的字段会更新,而当书籍更改时,仅更新格式。必须为编辑和格式列定义使用新作者或书籍的表单覆盖,否则这些字段在再次呈现时不会更改。
[示例结束]
在示例中,它指出“必须定义使用新作者或书籍的表单覆盖”。
问题是如何定义这些表单覆盖?
我已阅读 https://github.com/activescaffold/active_scaffold/wiki/Form 上的文档-覆盖,并尝试了不同的形式覆盖,但到目前为止没有运气,即列没有再次呈现。
如果您可以帮助我编写给定示例中所需的表单覆盖的代码,那么我应该能够将其移植到我的代码中。
Below example is taken from this documentation page:
https://github.com/activescaffold/active_scaffold/wiki/Chaining-Form-Fields
[Example start]
You can set an array of columns to update multiple columns when a column changes, and chain column updates:
class UsersController < ApplicationController
active_scaffold do |config|
config.columns[:author].form_ui = :select
config.columns[:author].update_columns = [:book, :editorial]
config.columns[:book].form_ui = :select
config.columns[:book].update_columns = :format
end
end
In this example, fields for book, editorial and format are updated when author changes, and when book changes only format is updated. A form override which use the new author or book must be defined for editorial and format columns, in other case those fields won’t change when they will be rendered again.
[Example end]
In the example it states "a form override which use the new author or book must be defined".
Question is how to define those form overrides ??
I have read the documentation on https://github.com/activescaffold/active_scaffold/wiki/Form-Overrides, and tried different form overrides, but with no luck so far, i.e. the columns are not being rendered again.
If you can help me with the code for those form overrides needed in the given example, then I should be able to port that to my code.
发布评论
评论(1)
这是我的问题的解决方案:
我按照“https://github 上的示例进行操作.com/activescaffold/active_scaffold/wiki/Chaining-Form-Fields”,但是当它对我的链接列不起作用时(更新第一列时,所有链接列都会正确更新,但更新第二列时)它的链接列呈现为空白列表),然后我(盲目地?)关注示例下面解释的细节,因为我认为这是解决我的问题的第一步:“使用新作者或书籍的表单覆盖必须是为编辑和格式列定义,在其他情况下,这些字段在再次呈现时不会更改”。
然而事实并非如此,不需要在帮助器中覆盖任何形式来使其工作,在帮助器中“options_for_association_conditions”就足够了。由于示例是针对 v2.4 的,因此在 v3.0+ 中可能不再需要表单覆盖。
解决方案在示例 wiki 的下一段中:“通常仅发送更改的列的值,如果您需要其他值来呈现更新的列,请启用 send_form_on_update_column 并且将发送所有表单”。我的问题是,从第二列链接的列也需要第一列的值,因此使用“send_form_on_update_column”设置第二列(即发送整个表单,而不仅仅是其自身的值)解决了我的问题。
在示例中,这将是:
Here is the solution to my problem:
I followed the example on "https://github.com/activescaffold/active_scaffold/wiki/Chaining-Form-Fields", but when it did not work for my chained columns (when updating the first column all chained columns updates correctly, but when updating the second column then its chained columns renders to blank lists), then I focused (blindly?) on the details explained just below the example as I thought this was the first step to solve my problem: "A form override which use the new author or book must be defined for editorial and format columns, in other case those fields won’t change when they will be rendered again".
This was however not the case, no form override in the helper was needed to get this to work, in the helper the "options_for_association_conditions" is enough. As the example is for v2.4, maybe the form override is not needed anymore in v3.0+.
The solution is in the next paragraph on the example wiki: "Usually only the value of the changed column is sent, if you need another values to render the updated columns, enable send_form_on_update_column and all the form will be sent". My problem was, that the columns which was chained from the second column needed the value from the first column also, so setting up the second column with "send_form_on_update_column" (i.e. sending the whole form, not just its own value) solved my problem.
In the example this would be: