4

there is something that is disturbing me a bit about rails 3. i am trying to upgrade my application from rails 2 to rails 3.

i followed all the steps necessary to do and things were working well for me till now except one thing:

i have a helper method that is defined differently in different helpers, for example it is defined in the application_helper in one way and the same method defined in a different way in the homepage_helper, and defined differently in another helper, let's say video_helper, in addition to that, it is used in a shared view that is used throughout the application, now if i am in the homepage, i want this function to get called from the homepage_helper and if i am in the video page, the function must be called from the video_helper, based on the concept of convention in rails, this worked perfectly in rails 2 but in rails 3, the function is always called from the last helper, alphabetically ordered, that is in the example, video_helper.

How can i fix this? Kindly advice.

Appreciate all the help.

2 Answers 2

2

In rails 3 all helpers are always loaded.

You can either:

1) name your helpers differently.

2) Explicity define which helpers you want

3) Check the controller from the helpers to determine which helper to provide.

2
  • Thanks a lot for your help, finally someone answered me :) option 1 will not work for my code, but how can i try the other two options? I tried including the helper methods but not one way worked for me, appreciate your help
    – Frida777
    Jan 9, 2011 at 12:59
  • For method 2, use clear_helpers and then specify the helpers you want to include as usual.
    – thomasfedb
    Jul 26, 2012 at 5:47
1

If I'm understanding your post right, you have something like

module ApplicationHelper
  def error_messages
    ...
  end
end

module VideoHelper
  def error_messages
    ...
  end
end

module HomepageHelper
  def error_messages
    ...
  end
end

And now you want to call error_messages from a specific helper and calling error_messages arbitrarily behaves differently from how it did in Rails 2. Am I right so far? If not, you can ignore the rest of this post =P

I just spent half an hour playing with the syntax and one of my projects in irb and the closest I could get was something along the lines of Posts::PostsHelper.helper_method, which works in irb if I 'include PostsHelper' first.

With that said, if you're willing to do something like Class::ClassHelper.helper_method, why not just rename "helper_method" so that it's unique to your ClassHelper? You're going to have to change each "helper_method" call to be specific to some helper anyways. I think this is a much cleaner solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.