Execute controller function from RJS


#1

Hi all,

I wonder if it is somehow possible to execture or redirect to a
controller function from RJS?

Right now I do:

def foo()
render :partial => ‘executeagain’
end

_executeagain.rhtml

This will loop I know :slight_smile:

How can I achieve the same result but without using a partial??

Regards,
Henrik


#2

page.redirect_to :action => ‘whatever’


#3

Guest wrote:

page.redirect_to :action => ‘whatever’

Great! And how would you send in parameters with that?
page.redirect_to :action => ‘whatever’,para1, para2 ,or?

Thanks


#4

Guest wrote:

page.redirect_to :action => ‘whatever’

BTW that did not work!
It sends me to another page which I don’t want. I just want to run a
controller function and stay on the current page…


#5

new Ajax.Request(’<%= url_for(:action => “/test/foo”, :what => “ever”)
%>’,
{asynchronous:true, evalScripts:true});


#6

Mick S. wrote:

new Ajax.Request(’<%= url_for(:action => “/test/foo”, :what => “ever”)
%>’,
{asynchronous:true, evalScripts:true});

I really don’t get it.

If I have (inline RJS for simplicity) can I do this?
render :update do |page|
new Ajax.Request(’<%= url_for(:action => “/test/foo”, :what =>
“ever”)
%>’, {asynchronous:true, evalScripts:true});
end

Looks error prone :slight_smile:

Thanks
Henrik


#7

Ajax.Request will trigger a controller action - you simply need to
decide when to call it - either as inline code in which case it will be
executed when the page is rendered, or in response to some event, eg
onClick=‘new Ajax.Request…’

Henrik Z. wrote:

Mick S. wrote:

new Ajax.Request(’<%= url_for(:action => “/test/foo”, :what => “ever”)
%>’,
{asynchronous:true, evalScripts:true});

I really don’t get it.

If I have (inline RJS for simplicity) can I do this?
render :update do |page|
new Ajax.Request(’<%= url_for(:action => “/test/foo”, :what =>
“ever”)
%>’, {asynchronous:true, evalScripts:true});
end

Looks error prone :slight_smile:

Thanks
Henrik


#8

Mick S. wrote:

Ajax.Request will trigger a controller action - you simply need to
decide when to call it - either as inline code in which case it will be
executed when the page is rendered, or in response to some event, eg
onClick=‘new Ajax.Request…’

Yes, I understand that, as I usually use it in partials for executing
controller functions.

My question is though how to call it from within a RJS template or
within inline RJS code?

What would the syntax be?
Is it possible at all or do I have to render a partial to be able to use
the Ajax.request call??

Thanks for your help
Henrik


#9

try something like this:

<%= periodically_call_remote( { :url => { :controller => “foo”, :action
=>
“bar” }, :frequency => 1 } ) %>

class FooController < ApplicationController

def bar
# do you thing here
render :nothing => true if request.xhr?
end
end


#10

In fact you just need to use the
[url=http://api.rubyonrails.com/classes/ActionView/Helpers/PrototypeHelper.html#M000420]remote_function
helper. If you omit the :update parameter, it generates an Ajax:Request
call instead of AjaxUpdater - eg:


#11

I guess you would use the << operator to output raw JavaScript to the
page - something like:

render :update do |page|
page << “new Ajax.Request(’#{url_for(:action => “/test/foo”, :what =>
“ever”)}’, {asynchronous:true, evalScripts:true});”
end

Sorry - I haven’t played around with RJS templates yet - embedding RJS
calls in partials has given me what I need.


#12

Mick S. wrote:

…or even:

render :update do |page|
page << remote_function(:url => {:action => :foo, :what => “ever”})
end

That did the trick!

I first got some weird results but thats because I hade the :update
option on the initial link_to_remote. :slight_smile:
Thanks for all your help! :slight_smile:

Cheers,
Henrik


#13

…or even:

render :update do |page|
page << remote_function(:url => {:action => :foo, :what => “ever”})
end