Pondering a more Rubyish Rails controller-view relationship.
I was playing around, trying to balance Ruby’s and Rails’ conventions, and came up with a slightly different way to share controller information with the view.
Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!
I was playing around, trying to balance Ruby’s and Rails’ conventions, and came up with a slightly different way to share controller information with the view.
Comments
It looks like you’re kind of defeating your point by making the controllers private method’s exposed. If your argument is that instance variable’s are being accessed outside of their intended OO scope then the same can be said about private methods.
Brian,
There’s no need to make the methods private. I address the choice, though:
What? [controller.#{helper_method}is] pretty ugly? And what about the controller public instance methods? They should refer to actions? I guess we can’t completely ignore Rails’ conventions. We’ll have to make those gettersprivate. But how, now, can we deliver these methods to the view? Luckily, Rails makes this easy with helpers.Basically, that choice is just to conform to some Rails conventions. If you think about it,
helper_methodis just something that makes methodspublicto the view. Not unlike:The entire article was meant as an exercise and halfway proof-of-concept. Regardless, it had a few nice side-effects: using helper methods basically delays the method calls so that if you are caching you don’t have needless database queries, and you don’t have to waste time scoping and maintaining your instance variable-setting
before_filters. For these reasons alone I don’t think my minor concession to convention is “defeating [my] point.”Oh I liked the article and it was interesting, but just remember that the helper_method isn’t making methods “public” to the view. It is most likely doing a send against the controller, which just so happens to be a work-around in Ruby to allow access to private/protected methods outside of their normal use.
Yes. That was a poor choice of words. I was just trying to make an analogy. There is certainly more going on behind the scenes; It’s just a question of explicitness.
@articles, set in a controller and seemingly just available in the view, is less explicit than callinghelper_methodto send methods to the view.Hi Stephen, I’m trying it now. I like the approach (like you I don’t like seeing instance variable everywhere) but I’m struggling a little to get the error handling right. In particular, how do you handle RecordNotFound exceptions?
Mike, feel free to email me with more detail on the subject (there’s a link on the bottom of my site).
Generally, using a helper method instead of an instance variable should yield identical results; the difference may arise, I suppose, is if you let instance variables silently fail if they aren’t populated. I think the preferred practice would be to seek out these RecordNotFound exception-raisers, and make sure that record should be available for the current action.
An alternative would be to call
Model.find_by_id(params[:id])instead ofModel.find(params[:id]).There are, likely, some core Rails methods that assume instance variables are being used (
error_messages_forbeing one of them, I believe), but there are usually workarounds (f.error_messagesin aform_for, for example).Post a comment