Call action, render it, then redirect back to itself


#1

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?


#2

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


#3

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.


#4

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 support to provide graphical feedback while the action
reloads. See <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


#5

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?