Forum: Ruby on Rails Execute controller function from RJS

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0377ec6fab3880beb5da3dde78af5418?d=identicon&s=25 Henrik Zagerholm (cubiq)
on 2006-04-27 11:11
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
<script>
new Ajax.Updater('elmntID', '/test/foo/', {evalScripts:true});
</script>


This will loop I know :)

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

Regards,
Henrik
60512389685fb9361e946969edddcdad?d=identicon&s=25 Guest (Guest)
on 2006-04-27 17:21
page.redirect_to :action => 'whatever'
0377ec6fab3880beb5da3dde78af5418?d=identicon&s=25 Henrik Zagerholm (cubiq)
on 2006-04-27 17:35
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
0377ec6fab3880beb5da3dde78af5418?d=identicon&s=25 Henrik Zagerholm (cubiq)
on 2006-04-27 17:51
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...
F5c118aaeb5bb7162db613a37e595be8?d=identicon&s=25 Mick Sharpe (micksharpe)
on 2006-04-27 18:13
new Ajax.Request('<%= url_for(:action => "/test/foo", :what => "ever")
%>',
                 {asynchronous:true, evalScripts:true});
0377ec6fab3880beb5da3dde78af5418?d=identicon&s=25 Henrik Zagerholm (cubiq)
on 2006-04-27 18:25
Mick Sharpe 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 :)

Thanks
Henrik
F5c118aaeb5bb7162db613a37e595be8?d=identicon&s=25 Mick Sharpe (micksharpe)
on 2006-04-27 18:49
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 Zagerholm wrote:
> Mick Sharpe 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 :)
>
> Thanks
> Henrik
F5c118aaeb5bb7162db613a37e595be8?d=identicon&s=25 Mick Sharpe (micksharpe)
on 2006-04-27 18:59
In fact you just need to use the
[url=http://api.rubyonrails.com/classes/ActionView/Help...
helper. If you omit the :update parameter, it generates an Ajax:Request
call instead of AjaxUpdater - eg:

<element onClick="<%= remote_function(:url => {:action => :foo, :what =>
'ever'}) >">
0377ec6fab3880beb5da3dde78af5418?d=identicon&s=25 Henrik Zagerholm (cubiq)
on 2006-04-27 19:02
Mick Sharpe 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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-04-27 19:20
(Received via mailing list)
try something like this:

<!-- call foo/bar every second -->
<%= 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
F5c118aaeb5bb7162db613a37e595be8?d=identicon&s=25 Mick Sharpe (micksharpe)
on 2006-04-27 19:24
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.
F5c118aaeb5bb7162db613a37e595be8?d=identicon&s=25 Mick Sharpe (micksharpe)
on 2006-04-27 19:26
...or even:

render :update do |page|
  page << remote_function(:url => {:action => :foo, :what => "ever"})
end
0377ec6fab3880beb5da3dde78af5418?d=identicon&s=25 Henrik Zagerholm (cubiq)
on 2006-04-27 22:15
Mick Sharpe 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. :)
Thanks for all your help! :)

Cheers,
Henrik
This topic is locked and can not be replied to.