RubyFlow The Ruby and Rails community linklog

×

The Ruby and Rails community linklog

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!

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.

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 getters private. 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_method is just something that makes methods public to the view. Not unlike:


private
  def test; "test" end

public :test

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.

...but just remember that the helper_method isn't making methods "public" to the view...

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 calling helper_method to 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 of Model.find(params[:id]).

There are, likely, some core Rails methods that assume instance variables are being used (error_messages_for being one of them, I believe), but there are usually workarounds (f.error_messages in a form_for, for example).

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in