简体   繁体   English

在Rails中定义应用程序范围的部分及其变量的正确方法是什么

[英]What is the correct way to define an application-wide partial and its variables in rails

I am building a rails app that has a number of models, one of which is Items. 我正在构建一个具有许多模型的Rails应用,其中一个是Items。

Items contains a short list of products and supporting information. 物品包含产品和支持信息的简短列表。

I need to display this information on every page in the app. 我需要在应用程序的每个页面上显示此信息。

In my views I've added 在我看来,我已添加

<%= render @items %>

and then added the partial 'items/_item.html.erb' 然后添加部分“ items / _item.html.erb”

Now, to get this all to work, I also need to define the variable 现在,要使所有这些正常工作,我还需要定义变量

@items = Item.all

My question is, what is the correct way to define this variable? 我的问题是,定义此变量的正确方法是什么? I could add this line to every view for every controller, but that doesn't seem very DRY. 我可以将此行添加到每个控制器的每个视图中,但这似乎不是很干。

Should I be defining this in the application controller? 我应该在应用程序控制器中定义它吗? If so, will this cause any issues if I also want to maintain access to the Items index page? 如果是这样,如果我还想保持对Items index页面的访问,这会引起任何问题吗?

Sorry if this is a simple question. 抱歉,这是一个简单的问题。 I'm trying to think through the best approach, and haven't found much written about this case. 我正在尝试通过最佳方法进行思考,但尚未找到有关此案例的大量文章。 Grateful for fresh ideas and perspective! 感谢新鲜的想法和观点!

Thank you! 谢谢!

Do definitely should not call database from the view. 绝对不要从视图中调用数据库。

You can create private method inside application controller, and use it as before_filter for all controllers where @items collection is required. 您可以在应用程序控制器内部创建私有方法,并将其用作需要@items集合的所有控制器的before_filter。 Most probably, you should customize actions where @items should be populated, like: 最有可能的是,您应该自定义应在其中填充@items的操作,例如:

before_filter :load_items, :only => [:show, :edit, update]

If you need to display something, you should consider doing it in the Application controller, instead of in all the different controllers, and the view parts you could do in the layouts (since that is common through out the app). 如果需要显示某些内容,则应考虑在Application控制器中而不是在所有不同的控制器中进行显示,并且可以在布局中执行视图部分(因为在整个应用程序中很常见)。 You can have the partial in the same folder (ie layouts), and call it in the layouts view as 您可以将局部文件放在同一文件夹(即布局)中,并在布局视图中将其命名为

<%= render :partial => 'partial_name' %>

And since you've done this at the application level, I don't think you'll required to render this partial from anywhere else in your app. 而且,由于您已经在应用程序级别完成了此操作,因此我认为您无需从应用程序中的其他任何位置渲染此部分图像。

I think the easiest thing to do is to build a new partial (let's say we call it "items_box"). 我认为最简单的方法是构建一个新的部分文件(假设我们将其称为“ items_box”)。 In your items/_items_box.html.erb , you define @items as Item.all, and supply whatever rendering code you need. 在您的items/_items_box.html.erb ,将@items定义为Item.all,并提供所需的任何渲染代码。 Then all you have to do is change <%= render @items %> to <%= render 'items/items_box' %> . 然后,您要做的就是将<%= render @items %>更改为<%= render @items %> <%= render 'items/items_box' %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM