我怎样才能干掉所有这些对 render :index 的调用?

发布于 2024-10-15 05:47:45 字数 496 浏览 1 评论 0原文

我有一堆带有渲染索引视图的方法的控制器。这导致我在大多数方法的末尾编写 render :index 。下面是一个示例

def index
  @models = Model.find(:all)
end

def new_models
  @models = Model.find_by_new(true)

  render :index
end

def old_models
  @models = Model.find_by_new(false)

  render :index
end

理想情况下,我只需将渲染代码移至后置过滤器中,但由于控制器在进入后置过滤器之前调用渲染,因此这不是一个选项。

我有大量这样的控制器,因此解决方案将删除大量重复的代码。

这个应用程序目前仍然是 Rails 2.3,但是,它将在未来一两个月内升级到 Rails 3。因此,虽然我更喜欢适用于 2.3 的技术,但仅限 Rails 3 的解决方案仍然会受到赞赏。

I have a bunch of controllers with methods that render the index view. This results in me writing render :index at the end of the majority of methods. Here's an example

def index
  @models = Model.find(:all)
end

def new_models
  @models = Model.find_by_new(true)

  render :index
end

def old_models
  @models = Model.find_by_new(false)

  render :index
end

Ideally, I would just move the render code into an after filter, but as the controller makes a call to render before going to the after filter that's not an option.

I have a large number of controllers like this so a solution would remove a lot of repeated code.

This app is currently still Rails 2.3, however, it will be upgraded to Rails 3 in the next month or two. So while I would prefer a technique that works on 2.3, Rails 3 only solutions would still be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红ご颜醉 2024-10-22 05:47:45

如果你真的想干掉这个动作,并且它很常见,那么你可以自己做一些元编程。首先使用此模块定义创建一个 render_with_index.rb 文件:

module RenderWithIndex
  def self.included klass
    klass.class_eval do
      def self.render_with_index * methods
        methods.each do |method|
          self.class_eval <<-EVAL
            alias :old_method :#{method}

            def #{method}
              old_method
              render :index
            end
          EVAL
        end
      end
    end
  end
end

然后将该模块包含在控制器中并定义应使用索引渲染的方法(确保在方法声明之后发生 render_with_index 调用。

include RenderWithIndex

def index
  @models = Model.find(:all)
end

def new_models
  @models = Model.find_by_new(true)
end

def old_models
  @models = Model.find_by_new(false)
end

render_with_index :new_models, :old_models

该模块现在允许您使用只需将索引模板添加到 render_with_index 调用即可。

If you really want to DRY up this action, and if it's very common, then you can do some meta-programming of your own. First create a render_with_index.rb file with this module definition:

module RenderWithIndex
  def self.included klass
    klass.class_eval do
      def self.render_with_index * methods
        methods.each do |method|
          self.class_eval <<-EVAL
            alias :old_method :#{method}

            def #{method}
              old_method
              render :index
            end
          EVAL
        end
      end
    end
  end
end

Then include that module in your controller and define the methods that should render with index (make sure the render_with_index call happens after your method declarations.

include RenderWithIndex

def index
  @models = Model.find(:all)
end

def new_models
  @models = Model.find_by_new(true)
end

def old_models
  @models = Model.find_by_new(false)
end

render_with_index :new_models, :old_models

This module now allows you to render any action with the index template simply by adding it to the render_with_index call.

你的呼吸 2024-10-22 05:47:45

对我来说看起来很干。恕我直言,如果您不想使用控制器方法特定的模板,那么提及您正在渲染哪个模板是一个好习惯。

如果您的渲染代码从一行扩展到多行代码,我会将它们干燥为单独的渲染方法。

Looks pretty DRY to me. IMHO it's a good habbit to mention which template you are rendering if you don't want to use controller method specific template.

If your render code extends from one-liner into several lines of code, I would DRY them up into separate render method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文