I want to have some RJS executed whenever an xhr request is made. My
initial thought was an "application.rjs" might do this, however that
does not seem to be the case.
Is there a way to set up some RJS that is executed whenever RJS is
fired? I have code that I don't want to repeat all over the place?
Failing the above. I thought adding a helper function the application.rb
and calling that from each .rjs file. However this does not work either.
Is there a way to call a method defined in applicaiton.rb from any .rjs
file?
Are you saying put something in my application controller (if so,
where?) or in the controller action method that is being fired before
the controller/action.rjs file is executed?
If it's the latter then that would mean repearing it on every action
that has RJS.
What I'm really looking for is if Rails has a way of invoking a common
RJS file on every RJS action.
My reasoning for this is there are "core" tasks that I'd like to do
using Ajax, such as showing/hiding my flash bar depending on whether
there is a message to display.
I want to have some RJS executed whenever an xhr request is made. My
initial thought was an "application.rjs" might do this, however that
does not seem to be the case.
Is there a way to set up some RJS that is executed whenever RJS is
fired? I have code that I don't want to repeat all over the place?
Failing the above. I thought adding a helper function the
application.rb
and calling that from each .rjs file. However this does not work
either.
Is there a way to call a method defined in applicaiton.rb from
any .rjs
file?
put this in your application.rb (or any appropriate helper file)
def some_common_stuff
update_page do |page|
page.some_rjs_stuff
...
end
end
It definitely should be application_helper (my bad, not quite typing what i was thing). It would be awfully helpful to know what the error was (check the log files)
> page << rjs_update_flash
> My log shows a 500 internal error.
> I also tried putting the method in application_helper.rb but that
> didn't
> work either.
This does work (on Rails 2.0.2 at least):
in application_helper.rb:
module ApplicationHelper
def display_flash(flash)
page.replace_html('FlashPane', flash[:notice])
end
end
in your "create.js.rjs" file:
page.display_flash(flash)
You need to pass the flash hash in specifically. I am using this now,
it works really nicely - not just for flash, you can put any RJS in
there you want.