如何根据页面更改渲染 @events 输出
在一个视图中,我有 render @events
,它使用 _event.html.erb
来生成输出。我想在另一个区域(主页)上使用render @events
,但我希望它以稍微不同的方式呈现页面。我该怎么做?
On one view I have render @events
which uses _event.html.erb
to make the output. I want to use render @events
on another area (homepage) but I want it to render the page slightly differently. How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据它们的不同程度,您可以使用演示者,如最新 Railscast 中所示。
如果差异是结构性的,则只需制作两个部分即可。视图中的少量重复是完全可以接受的,并且可能比在视图中放置逻辑更好。
尽量克制住在你的观点中创建 if 语句的冲动。
Depending on how different they are, you could use a presenter, like demonstrated in the latest Railscast.
If the difference is structural, just make two partials. A small amount of duplication in your views is totally acceptable and probably preferable to placing logic in your views.
Try to resist the urge to create if statements in your views.
我至少看到了这一点:
对于第一个选项(我将使用haml,我将使其变得简单):
更简单的东西,其中我相信这是 @iain 的建议之一:
这样你就可以干净地制作任意数量的不同部分,并在渲染调用上进行一些重复。我同意如果可能的话,您应该避免在视图中使用 if 语句。
我强烈建议参考指南,因为它们非常清晰且易于理解读。
这里的哈希是 Ruby 1.9 风格,如果您使用的是 Ruby 1.8,请将
collection: @events
替换为:collection =>; @events
希望这有帮助,抱歉对我的答案进行了大幅修改。
I see at least this:
For the first option (I'll use haml and I'll make it simple):
Something simpler, which I believe is one of @iain suggestions:
This way you can cleanly make as many different partials as you like, with a little repetition on the render call. I agree that you should avoid if statement in views if possible.
I strongly suggest to refer to the guides as they're very clear and easy to read.
Hash here are Ruby 1.9 style, if you're with Ruby 1.8 replace something like
collection: @events
with something like:collection => @events
Hope this helps, sorry for drastic edit of my answer.