Why, really, do we avoid putting view stuff in controllers? Why, that’s easy … because we’re talking visual elements here— things to lay out, things that aren’t necessarily a function of the application’s behavior or workflow, right? Things get muddled and then everything goes to hell, right?
Here’s the deal. I say replacing a portion of the page with another due to user interaction is behavioral. Granted, changing this or that element of the page can lead to some tight coupling, so it needs to be approached with caution … but there’s no glittering ivory tower here, folks.
A quick example, from a controller.
def add_item
Item.create(params[:item])
@items = Item.find(:all)
render :update do |page|
# Think "inline RJS"
page[:item_list].reload
end
end
Here, we have a listing of items; maybe they’re sorted alphabetically, so we want to go ahead and reload the whole listing (rather than just inserting the new item, which could also be done).
It’s pretty simple. If you know RJS and you look at this action, you know what’s going on— the whole process that occurs when the user submits the form. If the RJS is just an extension of the behavior that’s occurring in the controller, why put it in a separate file— why dig when you can just know?
Sometimes pure, rigid thoughts are just naive, dumb thoughts. Feel free to adhere to doctrine— but once it’s counter-productive, cut and run.
Ok, here’s the deal. I’m about to commit blasphemy. I’m going to recommend you put view-related code in the controller.
Wait a second. What about the whole MVC thing… never the twain shall meet, right? What kind of madness is this??!? (ducks lightning bolt)
Put down the pitchforks and torches, dammit, and listen.