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?
thanks!
in your controller try something like this.
format.js { render :template => “shared/error.js.rjs” }
John I
On Feb 16, 10:03 am, John L. [email protected]
Not sure I understand you John - can you clarify what you mean by
in your controller try something like this.
format.js { render :template => “shared/error.js.rjs” }
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.
Many thanks for your help.
On 16 Feb 2008, at 16:03, John L. wrote:
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
Then in your rjs files you can do
page << some_common_stuff
Fred
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
Then in your rjs files you can do
page << some_common_stuff
Fred
Here is what I tried. I put this method within my application.rb :
def rjs_update_flash
put “rjs update flash”
update_page do |page|
unless flash.empty?
if flash[:error]
page.replace_html(‘alert_panel_message’, “
#{flash[:error]}
”)
elsif flash[:notice]
page.replace_html(‘alert_panel_message’, “
#{flash[:notice]}
”)
end
page.show(‘alert_panel’)
else
page.hide(‘alert_panel’)
end
end
end
And, in my rjs file:
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.
Have I done something wrong?
This was modified from peepcodes RJS screencast ( I updated it for
rails 2)
http://peepcode.com/products/rjs-templates
They use a task list for the demo.
In the tasks_controller
POST /tasks
POST /tasks.xml
def create
@task = Task.new(params[:task])
respond_to do |format|
if @task.save
calculate_totals
flash[:notice] = 'Task was successfully created.'
format.html { redirect_to(@task) }
format.xml { render :xml => @task, :status
=> :created, :location => @task }
format.js
else
format.html { render :action => “new” }
format.xml { render :xml => @task.errors, :status
=> :unprocessable_entity }
format.js { render :template => “shared/error.js.rjs” }
end
end
end
PUT /tasks/1
PUT /tasks/1.xml
def update
@task = Task.find(params[:id])
respond_to do |format|
if @task.update_attributes(params[:task])
flash[:notice] = 'Task was successfully updated.'
format.html { redirect_to(@task) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @task.errors, :status
=> :unprocessable_entity }
format.js { render :template => “shared/error.js.rjs” }
end
end
end
When ever something is not saved or updated VIA AJAX if fires the
shared/error.js.rjs file
I gues this could be DRYed up.
the shared/error.js.rjs file just has
page.alert “An error happened!”
HTH
John Ivanodd
On Feb 18, 3:25 pm, John L. [email protected]
On 18 Feb 2008, at 21:25, John L. wrote:
work either.
Have I done something wrong?
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)
Fred
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.
Mikel