I have a model with attibutes name, title, description ect. If the name is by example weather-foracast I want to show an extra html element
我有一个名称,标题,描述等的模型。如果名称是例如weather-foracast我想要显示一个额外的html元素
= high_chart("my_id", @chart)
in the show template, wich generates a nice chart. When the page is not weather-foracast i don't want to show the chart.
在show模板中,它会生成一个漂亮的图表。当页面没有天气预报时,我不想显示图表。
What is the best approach? Conditions in the view?
什么是最好的方法?视野中的条件?
3 个解决方案
#1
1
Your problem looks really close to the Exhibit Pattern presented in Object on Rails.
您的问题看起来非常接近于Object on Rails中的Exhibit Pattern。
Simply explained, the solution proposed is to wrap your model in a decorator (or multiple decorators) chosen using the state of your model (in your case the name of the record).
简单地说,提出的解决方案是将模型包装在使用模型状态选择的装饰器(或多个装饰器)中(在您的情况下是记录的名称)。
I cannot explain it as well as it is explained in the book but that Pattern is so useful that Avdi Grimm made a gem out of it : https://github.com/objects-on-rails/display-case
我无法解释它,因为它在书中有解释,但是模式非常有用,以至于Avdi Grimm用它制作了一个宝石:https://github.com/objects-on-rails/display-case
#2
1
I know this answer is totally over the top, but something to consider is moving your conditional logic outside of your views.
Maybe going with the View Presenter pattern, see this: About presenter pattern in rails. is a better way to do it?
我知道这个答案完全超出了顶部,但需要考虑的是将条件逻辑移到视图之外。也许使用View Presenter模式,请参阅:关于rails中的演示者模式。这是一个更好的方法吗?
Presenters are ruby objects and can be tested really easily.
Whereas having tons of logic inside the view is really hard to test.
Furthermore, you want isolation of logic, checking for model.name inside your view page is not the best way of MVC.
演示者是红宝石对象,可以很容易地进行测试。而在视图中拥有大量逻辑确实很难测试。此外,您需要隔离逻辑,在视图页面内检查model.name并不是MVC的最佳方式。
#3
1
Since it looks like you're HAML, you can just use an if
modifier like so:
因为它看起来像你是HAML,你可以像这样使用if修饰符:
= high_chart("my_id", @chart) if [model.name] == "weather-forecast"
where [model.name]
is replaced with the appropriate code.
其中[model.name]被替换为适当的代码。
#1
1
Your problem looks really close to the Exhibit Pattern presented in Object on Rails.
您的问题看起来非常接近于Object on Rails中的Exhibit Pattern。
Simply explained, the solution proposed is to wrap your model in a decorator (or multiple decorators) chosen using the state of your model (in your case the name of the record).
简单地说,提出的解决方案是将模型包装在使用模型状态选择的装饰器(或多个装饰器)中(在您的情况下是记录的名称)。
I cannot explain it as well as it is explained in the book but that Pattern is so useful that Avdi Grimm made a gem out of it : https://github.com/objects-on-rails/display-case
我无法解释它,因为它在书中有解释,但是模式非常有用,以至于Avdi Grimm用它制作了一个宝石:https://github.com/objects-on-rails/display-case
#2
1
I know this answer is totally over the top, but something to consider is moving your conditional logic outside of your views.
Maybe going with the View Presenter pattern, see this: About presenter pattern in rails. is a better way to do it?
我知道这个答案完全超出了顶部,但需要考虑的是将条件逻辑移到视图之外。也许使用View Presenter模式,请参阅:关于rails中的演示者模式。这是一个更好的方法吗?
Presenters are ruby objects and can be tested really easily.
Whereas having tons of logic inside the view is really hard to test.
Furthermore, you want isolation of logic, checking for model.name inside your view page is not the best way of MVC.
演示者是红宝石对象,可以很容易地进行测试。而在视图中拥有大量逻辑确实很难测试。此外,您需要隔离逻辑,在视图页面内检查model.name并不是MVC的最佳方式。
#3
1
Since it looks like you're HAML, you can just use an if
modifier like so:
因为它看起来像你是HAML,你可以像这样使用if修饰符:
= high_chart("my_id", @chart) if [model.name] == "weather-forecast"
where [model.name]
is replaced with the appropriate code.
其中[model.name]被替换为适当的代码。