渲染局部时自动调用控制器方法 [英] Call a controller method automatically when rendering a partial
问题描述
我有一个部分需要运行一些控制器逻辑才能正常渲染.是否有某种方法可以将部分与一些在渲染时运行的控制器逻辑相关联?
I have a partial that needs to have some controller logic run before it can render without issue. Is there some way to associate the partial with some controller logic that is run whenever it is rendered?
例如,这是我当前代码的样子:
For example, this is what my current code looks like:
MyDataController:
MyDataController:
class MyDataController < ApplicationController
def view
@obj = MyData.find(params[:id])
run_logic_for_partial
end
def some_method_i_dont_know_about
@obj = MyData.find(params[:id])
# Doesn't call run_logic_for_partial
end
def run_logic_for_partial
@important_hash = {}
for item in @obj.internal_array
@important_hash[item] = "Important value"
end
end
end
view.html.erb:
view.html.erb:
Name: <%= @obj.name %>
Date: <%= @obj.date %>
<%= render :partial => "my_partial" %>
some_method_i_dont_know_about.html.erb:
some_method_i_dont_know_about.html.erb:
Name: <%= @obj.name %>
User: <%= @obj.user %>
<%# This will fail because @important_hash isn't initialized %>
<%= render :partial => "my_partial" %>
_my_partial.html.erb:
_my_partial.html.erb:
<% for item in @obj.internal_array %>
<%= item.to_s %>: <%= @important_hash[item] %>
<% end %>
如何确保在呈现 _my_partial.html.erb
时调用 run_logic_for_partial
,即使该方法未从控制器显式调用?如果我不能,那么 Rails 中是否有任何常用模式来处理此类情况?
How can I make sure that run_logic_for_partial
is called whenever _my_partial.html.erb
is rendered, even if the method isn't explicitly called from the controller? If I can't, are there any common patterns used in Rails to deal with these kinds of situations?
推荐答案
对于这种逻辑,您应该使用视图助手.如果您使用 rails generate
生成资源,则您的资源的帮助文件应该已经在您的 app/helpers
目录中.否则,您可以自己创建:
You should be using a views helper for this sort of logic. If you generated your resource using rails generate
, a helper file for your resource should already be in your app/helpers
directory. Otherwise, you can create it yourself:
# app/helpers/my_data.rb
module MyDataHelper
def run_logic_for_partial(obj)
important_hash = {}
for item in obj.internal_array
important_hash[item] = "Important value" // you'll need to modify this keying to suit your purposes
end
important_hash
end
end
然后,在您的部分中,将您要操作的对象传递给您的助手:
Then, in your partial, pass the object you want to operate on to your helper:
# _my_partial.html.erb
<% important_hash = run_logic_for_partial(@obj) %>
<% for item in important_hash %>
<%= item.to_s %>: <%= important_hash[item] %>
<% end %>
或者:
# app/helpers/my_data.rb
module MyDataHelper
def run_logic_for_partial(item)
# Do your logic
"Important value"
end
end
# _my_partial.html.erb
<% for item in @obj.internal_array %>
<%= item.to_s %>: <%= run_logic_for_partial(item) %>
<% end %>
正如 Ian Kennedy 所评论的那样,这个逻辑也可以合理地抽象为模型中的一个便捷方法:
As commented Ian Kennedy points out, this logic can also reasonably be abstracted into a convenience method in your model:
# app/models/obj.rb
def important_hash
hash = {}
for item in internal_array
important_hash[item] = "Important value"
end
hash
end
然后,您将在部分中以以下方式访问 important_hash
属性:
Then, you'd access the important_hash
attribute in the following manner in your partial:
# _my_partial.html.erb
<% for item in @obj.important_hash %>
<%= item.to_s %>: <%= item %>
<% end %>
这篇关于渲染局部时自动调用控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!