为这种类型的场景创建一个辅助方法是个好主意吗?
我的 html.erb 中很多地方都有这段代码。
<div id="left-nav">
<%= render :partial => 'tests/tests_left_menu' %>
</div>
为此类代码创建辅助方法是个好主意吗?
如何在 helper 中编写这段代码?
I have this code in my html.erb at many places.
<div id="left-nav">
<%= render :partial => 'tests/tests_left_menu' %>
</div>
Is it a good idea to create helper method for this type of code ?
How to write this code in helper ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我看到了一些适合您的情况的好策略。根据您项目的具体要求进行选择。
div#left-nav
及其内容放入另一个部分,例如tests/tests_left_menu_with_wrapper
。这可以为您节省几行代码。content_tag
相比有何优势。您最好使用部分或布局。I see a few good strategies to use in your situation. Pick and choose based on your project's specific requirements.
div#left-nav
and its contents into yet another partial liketests/tests_left_menu_with_wrapper
. This saves you a couple of lines.ActionController::Base.layout
method, you'll be able to skip writing the entire segment altogether.content_tag
. You're probably better off using partials or layouts.就我个人而言,我认为没有必要,而且我认为这更像是因为您没有使用其他工具,例如 haml 为了帮助减少 erb 文件中的行数,
只需 1 即可在 haml 中实现相同的代码行:
希望这有帮助=)
Personally i don't think there's a need to, and i think it's more like because you are not using other tools like haml to help reduce the number of lines in an erb files
the same code can be achieved in haml in just 1 line:
hope this helps =)
我想如果您在很多地方都有该代码,我会将
div
移动到部分中。如果您需要灵活地将tests_left_menu
放在 div 之外,在这种情况下我仍然会选择两个部分而不是一个助手。尽可能避免用 Ruby 编写 html :)I suppose if you have that code in many places I'd move the the
div
into the partial. If you need the flexibility to havetests_left_menu
outside of the div I'd still pick two partials over a helper in this scenario. Avoid writing html in Ruby when you can :)