Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1

Best approach - Conditions in the view


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

= 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.

What is the best approach? Conditions in the view?


Solution

  • Your problem looks really close to the Exhibit Pattern presented in Object on Rails.

    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