Forum: Ruby on Rails call action, render it, then redirect back to itself

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.
Nanyang Z. (Guest)
on 2007-04-23 01:06
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?
cammo (Guest)
on 2007-04-23 02:08
(Received via mailing list)
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>
Nanyang Z. (Guest)
on 2007-04-23 12:25
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.
RossW (Guest)
on 2007-04-23 20:26
(Received via mailing list)
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>
Nanyang Z. (Guest)
on 2007-04-23 23:55
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,
<script type="text/javascript">
<!--
function delayer(){
    window.location =
"/books;read?current_book=<%=@current_book%>?book_left=<%=
@book_left%>?start_time=<%=@start_time%>"
}
//-->
</script>
<%= @book_content%>

and this code at the bottom of the view:
<script type="text/javascript">
<!--
setTimeout('delayer()', 3000)
//-->
</script>

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?
This topic is locked and can not be replied to.