【问题标题】:What is the most effecient way to get and view commonly used data inside a Rails application?在 Rails 应用程序中获取和查看常用数据的最有效方法是什么?
【发布时间】:2012-10-05 16:37:43
【问题描述】:

我有三个模型都通过has_many :through 方法相互关联。

ProgramCategory
ProgramSubcategory
Program

在我的应用程序中,我需要经常使用 ProgramCategory.title、ProgramSubcategory.title 和 Program.title。可以说,会有一个动态侧边栏菜单,它看起来像这样:

|- Shows (ProgramCategory)
  |- Action (ProgramSubcategory)
    |- Lost (Program)
    |- Games of Thrones (Program)
    |- Dexter (Program)

因为我知道application_controllerapplication_helperpartials 的力量;我对将所有这些组合在一起以找到最合适的方式感到迷茫。

我应该在哪里以及如何调用我的模型?我应该在哪里构建我的方法以通过我的所有控制器访问它?我应该简单地创建一个部分并在application 布局中呈现它吗?

我需要一些专家的启发,请...

谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.2 relational-database rails-activerecord


    【解决方案1】:

    此导航栏不是您显示的数据的核心部分,它在所有页面中或多或少都相同。因此,它不属于控制器。

    将其设为辅助方法,并将其结果缓存在视图中:

    app/helpers/sidebar_helper.rb

    module SidebarHelper
      def sidebar_data
        # blahblah, use any tag helper (include it here if necessary)
        # just remember to 
      end
    end
    

    app/controllers/your_controller.rb

    class YourController < ApplicationController
      helper :sidebar
      # ...
    

    (或将辅助方法放入您的应用程序助手中以使其随处可用)

    app/views/application/_sidebar.html.haml

    - cache do
      # well, put here whatever you need to output the sidebar
      # use the sidebar_data, which should return precooked data on 
      # which you can build your nested <ul>s.
      # just indent it two spaces, so it's inside the "cache do" block
    

    app/views/application/_sidebar.html.erb

    <% cache do %>
      same as above, don't worry about indentation
    <% end -%>
    

    并在适当的地方包括部分

    <%= render 'sidebar' %>
    

    【讨论】:

    • 这种方法很有意义。我会试一试,然后告诉你结果。提前致谢
    • 嗨,我刚刚尝试过这种方式:首先按照您所说的创建了一个辅助方法。在其中,我创建了一个实例变量用于演示目的。我将助手侧边栏添加到我的 application_controller。每当我调用 sidebar_data 方法时,我都可以在任何我想要的地方显示数据。但是当我使用 时,什么都没有显示。我应该从配置中激活一些东西还是什么?
    • 请你回答我的问题好吗?
    • 也许您没有正确遵循缓存指南。确保您阅读并理解这一点:guides.rubyonrails.org/caching_with_rails.html
    • @scaryguy 你能在没有“cache do”sn-p 的情况下获得侧边栏吗?缓存的使用只是一个建议,可以优化sn-p的使用,但是你需要能够展示一些没有“缓存”部分的东西。
    【解决方案2】:

    如果此侧边栏显示在整个应用程序的每个视图中,您可以将其添加到您的应用程序布局中。然后在您的应用程序控制器中添加一个前置过滤器以获取数据,这将为从应用程序控制器继承的每个控制器中的每个操作执行。您还可以将其限制为特定操作,例如仅:index:show(针对每个控制器)。

    class ApplicationController < ActionController::Base
      before_filter :get_side_bar, :only => [:index, :show]
    
      def get_side_bar
        #@sidebar = some code
      end
    end
    

    然后您可以使用辅助方法(如果需要)并在您的应用程序布局中呈现@sidebar

    如果您需要为某些控制器跳过此操作,您也可以这样做,此操作将在过滤除其他控制器内的索引之外的任何内容之前跳过应用程序控制器:

    class OtherController < ApplicationController
      skip_before_filter :get_side_bar, :except => [:index]
    end
    

    【讨论】:

    • 在某些情况下我不需要侧边栏。您建议我使用 ':only' 属性,但我如何限制它以用于不同的控制器?我猜:仅适用于当前控制器的操作?
    • 在答案中添加了跳过代码。 :only 是可选的,如果您希望它在 90% 的操作上运行,则更容易不使用 :only 并在过滤之前跳过这 10% 的情况
    猜你喜欢
    • 2023-03-27
    • 2011-09-18
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 2015-06-26
    • 2012-01-09
    • 2011-06-05
    • 2022-01-11
    相关资源
    最近更新 更多