开发者

Best way to hook into a Rails 3 page lifecycle

开发者 https://www.devze.com 2023-04-12 21:46 出处:网络
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.

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开发者_如何学JAVA-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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号