【发布时间】:2013-01-19 13:34:52
【问题描述】:
我想在不同控制器的各种视图中使用以下部分。有没有办法将@person 替换为更通用的变量,该变量只需调用当前控制器的实例变量,不管它是@person 还是@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
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3