I have a simple controller that was created through a scaffold and has a “Show” function.
In the view that was created through the scaffold, I have an image that appears only on a certain condition.
I want to set or evaluate the condition in the controller and send it to the view.
I am not sure how to do that or whether this is the correct approach.
The condition should generally be dealt with in the view file using erb, or haml. If you update your question with the condition, then I’ll see about updating my answer to reflect it. For now, I’ll use a common condition.
Say you only want to show an image if an object is featured. Let’s imagine there is a featured field in your object that acts as a flag (1,0).
If this object is say an Article, we can then check the condition in the view file. The controller would obtain the article from the model:
..
Remember this is an example condition that is not necessarily correct. It is just to illustrate my initial approach.
I wouldn’t suggest you use javascript to hide/show depending on this condition, because you are then putting your logic in javascript, when it can be easily managed from within your view files.
If the condition is complex, you would then move it to the model, and perform something like:
..rather than having that complex condition in your controller file. This allows you to reuse the condition away from the specific controller and makes it more testable.