How to access view helpers from RJS helpers in Ruby on Rails?

271 views Asked by At

I had some common bits of RJS that I was repeating in multiple RJS files, and so wanted to put it into a helper.

I added a method to app/helpers/application_helper.rb just like I would normally for an html.erb file. The method works, except that it can't call view helpers that the RJS file could.

One of the lines that worked in the RJS file was:

notices = flash.collect { |type,msg| content_tag(:p, msg, :class => type) }

But content_tag is not accessible from the RJS helper. Why not, and how do I access it from there?

1

There are 1 answers

0
ecoologic On

what about something like this?

#application_helper.rb
def view_helpers
  ActionController::Base.helpers
end

def do_it
  view_helpers.content_tag :div, :class => 'yes'
end

You have to use view_helpers in any method you call from rjs too, which is not very nice, but I tried it in the model (got projects only in 2.3 and 3.1) and that worked...