我怎样才能干燥辅助方法声明

时间:2011-03-04 00:40:13

标签: ruby-on-rails ruby ruby-on-rails-3

我定义了很多帮助器,它们基本上都是一样的。

def subtitle(page_subtitle)
  content_for(:subtitle) { page_subtitle }
end

def header(page_header)
  content_for(:header) { page_header }
end

def auto_header(page_auto_header)
  content_for(:auto_header) { page_auto_header }
end

def header_image(page_header_image)
  content_for(:header_image) { page_header_image }
end

def bodyclass(page_bodyclass)
  content_for(:bodyclass) { page_bodyclass }
end

还有更多......

我的问题是如何干这个代码?

我尝试过这个但是我没有工作

content_for_helpers = ["title","subtitle","logocolor"]
content_for_helpers.each do |helper|
  def helper(helper)
    content_for(helper.parameterize.underscore.to_sym) { helper }
  end
end

1 个答案:

答案 0 :(得分:2)

def helper what
  content_for(what) { send "page_#{what}" }
end
相关问题