挂钩 Rails 3 页面生命周期的最佳方式
我有以下场景:我正在调整就地编辑库,以便能够在输入字段上编辑值后显示格式化值。
为此,我想存储在给定控制器操作上呈现的某些字段的 id,以便在需要时通过 AJAX 提供服务。
我希望能够在视图中执行以下操作:
<%= edit_in_place @object, :attribute do |value|
"Formatted display value is #{value}"
end
%>
因此,我为正在呈现的控件生成一个 UUID:
<input ... data-uuid="27b52850-d68f-012e-5dc8-28373723e63c" ...>
最终目标是保留一个 FormattingRules
哈希值,该哈希值将接受正在传递的块并分配它到 UUID,以便稍后在用户编辑该值后,调用 AJAX 方法来格式化该值:
http://server/format/27b52850-d68f-012e-5dc8-28373723e63c ?value=My+Value
触发以下代码:
def show
block = BestInPlace::FormattingRules[params[:id]]
render :text => block.call(params[:value])
end
以及 cod返回:
格式化的显示值是我的值,
这很棒。这样,我可以隐藏就地编辑输入并显示格式化值。
一切都已经正常,但是 FormattingRules
哈希值正在无限增长并且永远不会被清空。
我的问题是:有没有一种方法可以连接到 Rails 生命周期并使这个哈希更细化(也许是每个页面或会话?)并确保它在页面不再被使用后消失?
I have the following scenario: I am adapting a edit in place library to be able to display formatted values after the value is edited on an input field.
For that, I want to store the id for some fields being rendered on a given controller action to be served via AJAX whenever needed.
I want to be able to do the following in a view:
<%= edit_in_place @object, :attribute do |value|
"Formatted display value is #{value}"
end
%>
So, I generate a UUID for the control being rendered:
<input ... data-uuid="27b52850-d68f-012e-5dc8-28373723e63c" ...>
The ultimate goal is to keep a FormattingRules
hash that would take the block being passed and assign it to the UUID so I could later on, after the user has edited the value, call an AJAX method to format the value:
http://server/format/27b52850-d68f-012e-5dc8-28373723e63c?value=My+Value
The following code is triggered:
def show
block = BestInPlace::FormattingRules[params[:id]]
render :text => block.call(params[:value])
end
And the cod returns:
Formatted display value is My Value
Which is great. With this, I can hide the edit in place input and display the formatted value.
Everything works already, however the FormattingRules
hash is growing indefinitely and never being emptied.
My question is: is there a way I can hook into the Rails lifecycle and make this hash more granular (per page or session, maybe?) and make sure it's gone after the page is no longer being used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论