Candidate for RJS?

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.

Xavier N. wrote:

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?

Xavier N. wrote:

On Thu, Jun 11, 2009 at 10:19 PM, Tyler
Knappe[email protected] wrote:

If the real code is different please paste it.

Here is the code:

session[:next_week] = Date.today.cweek

    render :update do |page|
            page["Current_Week_div_1"].replace_html 

session[:next_week].to_s
page[“Week_div”].replace_html session[:next_week].to_s
page[“test_div_1”].replace_html “Test 1”
page[“test_div_2”].replace_html “Test 2”
page[“test_div_3”].replace_html “Test 3”
page[“test_div_4”].replace_html “Test 4”
page[“test_div_5”].replace_html “Test 5”
page[“test_div_6”].replace_html “Test 6”
page[“test_div_7”].replace_html “Test 7”
for i in 0…23 do
for j in 0…6 do
if
(Date.today.beginning_of_week.to_time.to_i + ( j * 86400) + ( i * 3600)

  • ((session[:next_week] - session[:current_week]) * 7 * 86400 )) <
    (session[:lab_object].end_time.to_time.to_i) &&
    (Date.today.beginning_of_week.to_time.to_i + ( j * 86400) + ( i * 3600)
  • ((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

Tyler K. wrote:

Xavier N. wrote:

On Thu, Jun 11, 2009 at 10:19 PM, Tyler
Knappe[email protected] wrote:

If the real code is different please paste it.

Here is the code:

Does anyone know why this would only render a single element? Does
anyone have a better way of doing this?

Thanks,

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.

If the real code is different please paste it.

2009/6/11 Tyler K. [email protected]:

        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

Here is the code:

Does anyone know why this would only render a single element? Does
anyone have a better way of doing this?

in your loop you’re only updating the element with id test_div

Fred

Colin L. wrote:

2009/6/11 Tyler K. [email protected]:

        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.

2009/6/12 Tyler K. [email protected]:

    <div id = “test_div_” + ij"" style = “display :inline;”>
Available

Assuming you are in erb then

or similar
should work

Colin

2009/6/12 Frederick C. [email protected]:

@Fred - I suppose if pushed I might reluctantly agree that your
suggestion has some validity. I think the moral is Engage brain before
typing.

Colin

On Jun 12, 4:31 pm, Colin L. [email protected] wrote:

   <th><div id = "test_div_" + ij"" style = "display :inline;">

Available

Assuming you are in erb then

or similar
should work

I think you meant <%= i%> rather than #{i}

Fred

Frederick C. wrote:

On Jun 12, 4:31�pm, Colin L. [email protected] wrote:

� � � �<div id = “test_div_” + ij"" style = “display :inline;”>
Available

Assuming you are in erb then

or similar
should work

I think you meant <%= i%> rather than #{i}

Fred

This is the method I used.

Thanks guys!