渲染其他控制器'模板(Ruby on Rails)
我正在构建一个示例站点来熟悉 RoR。我在某种程度上遵循了《Agile Web Development with Rails》这本书,现在我正在尝试并使用它作为参考,但是我还没有找到问题的答案。
我感兴趣的有两种模式,一种是连锁超市,另一种是超市。显然,一个连锁超市有很多个超市。我想做的是获得单个连锁超市的基本“显示”页面,以显示属于它的超市列表。
我也不想重复自己(因为显然这是一件非常糟糕的事情),所以我想我可以使用“render”将超市的index.html.erb插入supermarketchain/show.html.erb页面,就像这样:
<%= render :template => 'supermarkets/index' %>
但是,这在页面上产生了零输出。
我的下一个方法是制作部分:
<div id="supermarket-list">
<h1><%= I18n.t "supermarket.title" %></h1>
<table>
<% for s in @supermarkets %>
<tr class="<%= cycle('list-line-odd', 'list-line-even') %>">
<td>
<%= s.supermarketchain.name %>
</td>
<td>
<%= s.address %>
</td>
<td class="list-actions">
<%= link_to I18n.t("general.show"), s%> <br/>
<%= link_to I18n.t("general.edit"), edit_supermarket_path(s) %> <br />
<%= link_to I18n.t("general.delete"), s, :confirm => I18n.t("general.confirmation"), :method => :delete %>
</td>
</tr>
<%end%>
</table>
</div>
然后使用:
<% @supermarkets = Supermarket.all %>
<%= render :partial => 'supermarkets/supermarket' %>
将其插入连锁超市的显示页面。
我想知道这是否是一个好的做法。在我看来,当我想要显示的是超市控制器的“索引”操作的确切结果时,初始化一个供部分使用的变量似乎很奇怪。评论?
请询问任何需要的澄清。
I am building a sample site to familiarize myself with RoR. I followed the book "Agile Web Development with Rails" up to a point, and now I am experimenting and using it as a reference, however I haven't been able to find the answer to my problem.
I have two models of interest, one is supermarketchain and the other supermarket. Obviously, a supermarket chain has a bunch of supermarkets. What I am trying to do is get the basic "show" page for a single supermarket chain to display a list of the supermarkets that belong to it.
I also didn't want to repeat myself (because apparently it's a Very Bad Thing), and so I thought I could use "render" to insert supermarket's index.html.erb into the supermarketchain/show.html.erb page, like this:
<%= render :template => 'supermarkets/index' %>
However, that produced zero output on the page.
My next approach was to make this partial:
<div id="supermarket-list">
<h1><%= I18n.t "supermarket.title" %></h1>
<table>
<% for s in @supermarkets %>
<tr class="<%= cycle('list-line-odd', 'list-line-even') %>">
<td>
<%= s.supermarketchain.name %>
</td>
<td>
<%= s.address %>
</td>
<td class="list-actions">
<%= link_to I18n.t("general.show"), s%> <br/>
<%= link_to I18n.t("general.edit"), edit_supermarket_path(s) %> <br />
<%= link_to I18n.t("general.delete"), s, :confirm => I18n.t("general.confirmation"), :method => :delete %>
</td>
</tr>
<%end%>
</table>
</div>
And then use:
<% @supermarkets = Supermarket.all %>
<%= render :partial => 'supermarkets/supermarket' %>
To insert it on the supermarketchain's show page.
What I am wondering is whether this is a good practice. It seems to me weird to initialize a variable for use by a partial, when what I want displayed is the exact result of the "index" action of the supermarkets controller. Comments?
Please ask for any needed clarifications.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Rails 中渲染来自其他控制器的部分效果非常好。通常,如果某个部分实际上不属于任何一个特定控制器,则会将其放入
app/views/shared
中。在这种情况下,我认为将部分内容保留在超市控制器中是有意义的。以下是您如何为这两个部分使用相同的部分
supermarkets/_supermarket.html.erb
。该部分将具有可用的局部变量supermarket
。It's perfectly fine in Rails to render partials from other controllers. Commonly if a partial doesn't really belong to any one particular controller, it is put in
app/views/shared
. In this case it makes sense to keep the partial with the supermarket controller though I think.Here's how you could make use of the same partial
supermarkets/_supermarket.html.erb
for both sections. The partial would have the local variablesupermarket
available.相反,您可以这样做,
并且将使用变量名称
s
对每个超市调用一次该部分。You can, instead, do
and the partial will be called once for every supermarket, with variable name
s
.好的,如果我猜对了,你就会得到一个连锁店列表,每个连锁店都有一个超市列表,对吗?您在这两个模型之间有关联设置吗?因为如果你有的话,那只会简化事情。在这种情况下,假设您将模型从“supermarketchains”重命名为“chain”(消除任何混淆)。您可以像这样关联模型>
has_many :supermarkets
并在超市模型中。
own_to :chain
从那里开始,这只是一个循环问题,例如:
也就是说,如果您没有设置控制器来提取该信息,则可能需要额外的工作。
Okay, so if I get this right, you have a list of chains and each chain has a list of supermarkets, right? Do you have an association setup between the two models? Because that would just simplify things if you have. In this case, assuming that you rename the model from "supermarketchains" to just "chain" to (clear any confusion). You can associate the models like so>
has_many :supermarkets
And in the supermarket model.
belongs_to :chain
From there, it's just an issue of looping like:
That said, if you haven't setup controllers to pull that info, it may take extra work.
我自己也曾为此苦苦挣扎,但发现它并没有真正被支持作为渲染部分的方式,而且由于我通常信任 Rails 开发人员约定,所以我认为这可能不是一个好主意。
我不认为在那里使用模板会起作用,你也许可以使用 render_to_string ,但这看起来很黑客,并且可能会在将来给你带来更多的麻烦。
另外,如果你想渲染一个集合,最好的方法是在控制器动作中设置实例变量,而不是在视图中。
然后在视图
index.html.erb
中,调用:然后,它将迭代数组的每个成员,并为每个成员渲染
views/supermarkets/_supermarket.html.erb
,其中“supermarket”是部分中当前可用的成员。我担心很多渲染的性能,但只要留意日志和时间,我认为渲染的时间与在原始视图中手动执行的时间大致相同。
I've struggled with this myself, but found that's it's not really supported as a way to render partials, and since I trust the Rails developers conventions usually, I decided that it probably wasn't a good idea.
I don't think using a template in there will work, you could maybe use
render_to_string
, but this seems hackish, and could cause you more headache in the future.Also, if you want to render a collection, the best way is to set the instance variable in the controller action, not in the view..
And then in the view
index.html.erb
, call:Which will then iterate over every member of the array, and render
views/supermarkets/_supermarket.html.erb
for each, with 'supermarket' being the current member available in the partial.I worry about the performance of a lot of these renders, but just keep an eye on the logs and the times, I think the times of render vs. doing it manually in the original view are about the same.