我想在不同控制器的各种视图中使用以下部分。有没有办法@person
用一个更通用的变量来替换,它只是调用当前控制器的实例变量,不管它是@person
or@user
还是@company
什么?
<% content_for(:timestamps) do %>
<p>Created: <%= l @person.created_at, :format => :long %>, Last updated: <%= l @person.updated_at, :format => :long %></p>
<% end %>
谢谢你的帮助。
这是我基于 Xavier 代码的辅助函数:
def timestamps
content_for(:timestamps) do
current_string = controller.controller_name.singularize
current_object = instance_variable_get("@#{current_string}")
created_at_time = l(current_object.created_at, :format => :long)
updated_at_time = l(current_object.updated_at, :format => :long)
text = "Created: #{created_at_time}, Last updated: #{updated_at_time}"
content_tag(:p, text)
end
end