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?
在一个视图中,我渲染了使用_event.html.erb来生成输出的@events。我想在另一个区域(主页)上使用render @events,但我希望它以稍微不同的方式呈现页面。我怎么做?
2 个解决方案
#1
0
I see at least this:
我至少看到这个:
- use css which you can use to position elements, hide others, colors etc
- 使用css可以用来定位元素,隐藏其他元素,颜色等
- render different partials in each template
- 在每个模板中渲染不同的部分
For the first option (I'll use haml and I'll make it simple):
对于第一个选项(我将使用haml,我会简单):
# events.html.haml
.index= render @events
# homepage.html.haml
.events= render @events
# your css
.index { color: red; }
.events { color: blue; }
Something simpler, which I believe is one of @iain suggestions:
更简单的东西,我认为是@iain建议之一:
# action template
= render partial: '/events/event1', collection: @events
# _event1.html.haml
.name #{event1.name}
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
Hash这里是Ruby 1.9风格,如果你使用Ruby 1.8替换类似集合的东西:@events类似于:collection => @events
Hope this helps, sorry for drastic edit of my answer.
希望这有帮助,抱歉我的答案的大幅编辑。
#2
1
Depending on how different they are, you could use a presenter, like demonstrated in the latest Railscast.
根据它们的不同,您可以使用演示者,如最新的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.
如果差异是结构性的,那就做两个部分。视图中的少量重复是完全可以接受的,并且可能比在视图中放置逻辑更可取。尽量抵制在视图中创建if语句的冲动。
#1
0
I see at least this:
我至少看到这个:
- use css which you can use to position elements, hide others, colors etc
- 使用css可以用来定位元素,隐藏其他元素,颜色等
- render different partials in each template
- 在每个模板中渲染不同的部分
For the first option (I'll use haml and I'll make it simple):
对于第一个选项(我将使用haml,我会简单):
# events.html.haml
.index= render @events
# homepage.html.haml
.events= render @events
# your css
.index { color: red; }
.events { color: blue; }
Something simpler, which I believe is one of @iain suggestions:
更简单的东西,我认为是@iain建议之一:
# action template
= render partial: '/events/event1', collection: @events
# _event1.html.haml
.name #{event1.name}
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
Hash这里是Ruby 1.9风格,如果你使用Ruby 1.8替换类似集合的东西:@events类似于:collection => @events
Hope this helps, sorry for drastic edit of my answer.
希望这有帮助,抱歉我的答案的大幅编辑。
#2
1
Depending on how different they are, you could use a presenter, like demonstrated in the latest Railscast.
根据它们的不同,您可以使用演示者,如最新的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.
如果差异是结构性的,那就做两个部分。视图中的少量重复是完全可以接受的,并且可能比在视图中放置逻辑更可取。尽量抵制在视图中创建if语句的冲动。