I have a function in my controller that loops over a div tag replacing
some text based up an updated variable set by the user.
Code:
===== Controller ======
render :update do |page|
for i in 0..23 do
for j in 0..6 do
if ([date calculation done here])
page["test_div"].replace_html
“Checked Out”
else
page[“test_div”].replace_html
“Available”
end
end
end
end
==========
When the function is called, it only replaces a single element out of
the loop (the first). Is there a way to render all elements? It sounds
like RJS is the way to do this (Render :update and best practices - Rails - Ruby-Forum)
but I still don’t see how.
On Thu, Jun 11, 2009 at 7:59 PM, Tyler
Knappe[email protected] wrote:
            for j in 0…6 do
==========
When the function is called, it only replaces a single element out of
the loop (the first). Â Is there a way to render all elements? Â It sounds
like RJS is the way to do this (Render :update and best practices - Rails - Ruby-Forum)
but I still don’t see how.
It is not a good pratice to use render :update like that. When the RJS
is more than a one-liner, say, it is more clean to extract that to its
own RJS template. Because RJS belongs to the V in MVC.
On the other hand the idea of the loop + calls to page…something is
correct. In your example the ID of the element is always the same
though. Knowing this, you could for example simplify the code like
eliminating the conditional to get a wrong but working version, and
after that add all the logic.
On Thu, Jun 11, 2009 at 7:59 PM, Tyler
Knappe[email protected] wrote:
            for j in 0…6 do
==========
When the function is called, it only replaces a single element out of
the loop (the first). Â Is there a way to render all elements? Â It sounds
like RJS is the way to do this (Render :update and best practices - Rails - Ruby-Forum)
but I still don’t see how.
It is not a good pratice to use render :update like that. When the RJS
is more than a one-liner, say, it is more clean to extract that to its
own RJS template. Because RJS belongs to the V in MVC.
On the other hand the idea of the loop + calls to page…something is
correct. In your example the ID of the element is always the same
though. Knowing this, you could for example simplify the code like
eliminating the conditional to get a wrong but working version, and
after that add all the logic.
Right, I understand the MVC logic. However, even removing the logic
from the loop only renders a single time; the first element. If I swap
how the loop is performed to loop over the :update then I get an error
about attempting more than one render per call. The same is also true
of redirects. Is there a better way of doing this, or will RJS fix the
problem? Can it perform more than one render?
((session[:next_week] - session[:current_week]) * 7 * 86400 )) >
(session[:lab_object].start_time.to_time.to_i - 3600)
page[“test_div”].replace_html
“Checked Out”
else
page[“test_div”].replace_html
“Available”
end
end
end
end
Executing this code only updates a single element.
BEFORE:
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
0:00 Available Available Available Checked Out Checked Out Checked Out
Checked Out
1:00 Available Available Available Checked Out Checked Out Checked Out
Checked Out
[this continues for an entire week]
AFTER:
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
0:00 Checked Out Available Available Checked Out Checked Out
Checked Out Checked Out
1:00 Available Available Available Checked Out Checked Out Checked Out
Checked Out
On Thu, Jun 11, 2009 at 10:19 PM, Tyler
Knappe[email protected] wrote:
Right, I understand the MVC logic. Â However, even removing the logic
from the loop only renders a single time; the first element. Â If I swap
how the loop is performed to loop over the :update then I get an error
about attempting more than one render per call. Â The same is also true
of redirects. Â Is there a better way of doing this, or will RJS fix the
problem? Â Can it perform more than one render?
That code is using RJS, that’s what provides you the page object. It
is inline RJS instead of RJS in its own file, but it is RJS.
Render can happen only once, but several calls to the page object in
the same render do not account for serveral renders.
Your code uses a single ID, if you modify only one element, that’s what
you get.
        page[“test_div_7”].replace_html “Test 7”
“Checked Out”
                else
                    page[“test_div”].replace_html
“Available”
                end
            end
        end
    end
Executing this code only updates a single element.
I am presumably missing something here, as I do not know much about
RJS, but inside the loop you seem to be replacing the html for the
same div (“test_div”) every time round the loop.
        page[“test_div_7”].replace_html “Test 7”
“Checked Out”
                else
                    page[“test_div”].replace_html
“Available”
                end
            end
        end
    end
Executing this code only updates a single element.
I am presumably missing something here, as I do not know much about
RJS, but inside the loop you seem to be replacing the html for the
same div (“test_div”) every time round the loop.
Colin
Oh. :facepalm:
Does anyone know how to dynamically generate div tags?
I tried this:
Available
Which didn’t work, rather it gives me this in the source:
<th><div id = "test_div_" + ij"" style = "display :inline;">
Available
The idea being that I would be able to use my loop to generate the div
tags.