Possible to restrict a helper to a view?

Right now I have two controllers:

courses_controller.rb
events_controller.rb

They each, of course, have their own helpers:

courses_helper.rb
events_helper.rb

Is it possible to create a helper method with the same name in these
two helpers such that they are called by the corresponding view?

Basically I want:

<%= meets_on Schedule.find(:first) %> in courses/index.html.erb to
call meets_on in courses_helper.rb and

<%= meets_on Schedule.find(:first) %> in events/index.html.erb to
call meets_on in events_helper.rb.

I thought that’s what would happen, but it turns out that the meets_on
method from events_helper.rb gets called all the time.

The helpers are unique to the controllers. Two methods defined in two
helpers should not conflict with or override each other. Are you perhaps
including EventsHelper in courses?

On Dec 21, 2007 5:06 PM, vince [email protected] wrote:

I thought that’s what would happen, but it turns out that the meets_on
method from events_helper.rb gets called all the time.


Ryan B.
http://www.frozenplague.net

I could have sworn that helpers are unique to controllers too. To
test it, I created a new project from scratch with just two
controllers for courses and events that created two helpers
courses_helper.rb and events_helper.rb. I put in a very simple method
in each:

courses_helper.rb -
module CoursesHelper
def my_helper
“hello courses”
end
end

events_helper.rb -
module EventsHelper
def my_helper
“hello events”
end
end

Then in views/courses/index.html.erb I put in just one line -
<%= my_helper %>

Now when I go to http://localhost:3000/courses I see this:
hello events

Can someone tell me what I’m doing wrong? I put up the sample project
here -
http://brainassembly.org/helper_trouble

The 3 files I mentioned above are here -
http://brainassembly.org/helper_trouble/app/views/courses/index.html.erb
http://brainassembly.org/helper_trouble/app/helpers/courses_helper.rb
http://brainassembly.org/helper_trouble/app/helpers/events_helper.rb

The whole project can be downloaded here -
http://brainassembly.org/helper_trouble.zip

Thanks for any help!

haha. thanks. it works now :slight_smile:

i guess i was thinking that something like this would be in
application_helper.rb but good to know!

On Dec 22, 2:09 pm, Nathan E. <rails-mailing-l…@andreas-

You may want to look at “application.rb” because it will answer your
question. Note the line in that file:

helper :all # include all helpers, all the time

You might guess what that does :wink:

Remove that line and all will be well.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs