# Call action, render it, then redirect back to itself

**URL:** <https://www.ruby-forum.com/t/call-action-render-it-then-redirect-back-to-itself/92673>\
**Category:** Rails\
**Created:** [April 22, 2007, 11:06pm UTC](https://www.ruby-forum.com/t/call-action-render-it-then-redirect-back-to-itself/92673 "2007-04-22T23:06:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [April 22, 2007, 11:06pm UTC](https://www.ruby-forum.com/t/call-action-render-it-then-redirect-back-to-itself/92673/1 "2007-04-22T23:06:20Z")

</div>

there is a controller action

> def loop\_type\_action  
> book = Book.find\_one\_book\_that\_left #find a book haven’t read  
> book.update\_attribute(“is\_read”,true) #mark it as read  
> @result = book.read #fetch book content  
> end

When this action is called,I want the @result be shown on the browser,  
then AFTER the @result is shown, I want it call this action again and do  
the same thing to show next book.

I’ve tried redirect\_to method, but it won’t give the chance to show  
anything on the browser before it redirect the action.

Any idea to do this?

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [April 23, 2007, 12:08am UTC](https://www.ruby-forum.com/t/call-action-render-it-then-redirect-back-to-itself/92673/2 "2007-04-23T00:08:00Z")

</div>

Why don’t you call all of the unread books up into an array and then  
use javascript to walk through the array, either timed or upon a  
button click in the front end. You could even use AJAX to post the  
update\_attributes to the server after the book has been read, rather  
than making tons of database calls, you could make 1.

Cam

On Apr 23, 7:06 am, Nanyang Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [April 23, 2007, 10:25am UTC](https://www.ruby-forum.com/t/call-action-render-it-then-redirect-back-to-itself/92673/3 "2007-04-23T10:25:04Z")

</div>

cammo wrote:

> Why don’t you call all of the unread books up into an array and then  
> use javascript to walk through the array, either timed or upon a  
> button click in the front end. You could even use AJAX to post the  
> update\_attributes to the server after the book has been read, rather  
> than making tons of database calls, you could make 1.

thanks cam.

in fact, the read book action just a example to explain my need. I use  
it because its simplicity. In really action, that will be taken into  
use, will include more Model operations.

What I really need is a loop action (as I said, may including many Model  
operations), with realtime(or at lease, kind of “live”), front end view  
(showing  
which loop is going on, how many loops left, is an error happen? what  
massage it gives if any error does happen…)

so, your idea is really good when I want a read book app, but it seems  
not fix my current app. Tons of database calls are needed.

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [April 23, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/call-action-render-it-then-redirect-back-to-itself/92673/4 "2007-04-23T18:26:27Z")

</div>

I would achieve this by calling

def loop\_type\_action  
book = Book.find\_one\_book\_that\_left #find a book haven’t read  
book.update\_attribute(“is\_read”,true) #mark it as read  
@result = book.read #fetch book content  
headers[“Refresh”] = “5; URL=” + url\_for(:action =\>  
“loop\_type\_action”)  
end

This will cause the page to refresh every 5 seconds, calling  
loop\_type\_action each time. You can change the number to the time you  
desire. You can also use expires\_now() in the controller action and  
putting link\_to(:action =\> “loop\_type\_action”) in the view on an  
element like the book title or cover (in this example). Every time the  
user clicks, the page should refresh. expires\_now causes it to not be  
cached.

I can also think of some Javascript things you could do to refresh the  
page only when certain conditions are true. You could also use Rails’  
[script.aculo.us](http://script.aculo.us) support to provide graphical feedback while the action  
reloads. See \<[http://api.rubyonrails.org/classes/ActionView/Helpers/](http://api.rubyonrails.org/classes/ActionView/Helpers/)  
ScriptaculousHelper.html#M000515\>.

Cheers,

Ross

On Apr 23, 4:25 am, Nanyang Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [April 23, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/call-action-render-it-then-redirect-back-to-itself/92673/5 "2007-04-23T21:55:48Z")

</div>

RossW wrote:

> I can also think of some Javascript things you could do to refresh the  
> page only when certain conditions are true.

Thanks RossW,  
I have just done a javascript test. I put this code at the up part of  
the view page,

\<%= @book\_content%\>

and this code at the bottom of the view:

It works fine. But I still have a question:  
when exactly will the delayer function be called? 3 seconds after the  
page is open or 3 seconds after the controller method have been taken  
and @book\_content is shown?
