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 (http://www.ruby-forum.com/topic/135158) but I still don''t see how. -- Posted via http://www.ruby-forum.com/.
On Thu, Jun 11, 2009 at 7:59 PM, Tyler Knappe<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 (http://www.ruby-forum.com/topic/135158) > 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 Noria wrote:> On Thu, Jun 11, 2009 at 7:59 PM, Tyler > Knappe<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 (http://www.ruby-forum.com/topic/135158) >> 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? -- Posted via http://www.ruby-forum.com/.
On Thu, Jun 11, 2009 at 10:19 PM, Tyler Knappe<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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.
Xavier Noria wrote:> On Thu, Jun 11, 2009 at 10:19 PM, Tyler > Knappe<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 <b>Checked Out</b> Available Available Checked Out Checked Out Checked Out Checked Out 1:00 Available Available Available Checked Out Checked Out Checked Out Checked Out -- Posted via http://www.ruby-forum.com/.
Tyler Knappe wrote:> Xavier Noria wrote: >> On Thu, Jun 11, 2009 at 10:19 PM, Tyler >> Knappe<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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, -- Posted via http://www.ruby-forum.com/.
2009/6/11 Tyler Knappe <rails-mailing-list@andreas-s.net>:> > Xavier Noria wrote: >> On Thu, Jun 11, 2009 at 10:19 PM, Tyler >> Knappe<rails-mailing-list@andreas-s.net> 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.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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > > 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> > Thanks, > -- > Posted viahttp://www.ruby-forum.com/.
Colin Law wrote:> 2009/6/11 Tyler Knappe <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: >> >> 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. > > ColinOh. :facepalm: Does anyone know how to dynamically generate div tags? I tried this: <th><div id = "test_div_" + ij"" style = "display :inline;"> Available </div></th> Which didn''t work, rather it gives me this in the source: <th><div id = "test_div_" + ij"" style = "display :inline;"> Available </div></th> The idea being that I would be able to use my loop to generate the div tags. -- Posted via http://www.ruby-forum.com/.
2009/6/12 Tyler Knappe <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:>... > > Does anyone know how to dynamically generate div tags? > > I tried this: > > <th><div id = "test_div_" + ij"" style = "display :inline;"> Available > </div></th> > > Which didn''t work, rather it gives me this in the source: > > <th><div id = "test_div_" + ij"" style = "display :inline;"> > Available </div></th>Assuming you are in erb then <div id="test_div_#{i}#{j}" > or similar should work Colin
On Jun 12, 4:31 pm, Colin Law <colinandyvo...-PMmbdagmEjUDXYZnReoRVg@public.gmane.org> wrote:> 2009/6/12 Tyler Knappe <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: > > Does anyone know how to dynamically generate div tags? > > > I tried this: > > > <th><div id = "test_div_" + ij"" style = "display :inline;"> Available > > </div></th> > > > Which didn''t work, rather it gives me this in the source: > > > <th><div id = "test_div_" + ij"" style = "display :inline;"> > > Available </div></th> > > Assuming you are in erb then <div id="test_div_#{i}#{j}" > or similar > should workI think you meant <%= i%> rather than #{i} Fred> > Colin
2009/6/12 Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > > > On Jun 12, 4:31 pm, Colin Law <colinandyvo...-PMmbdagmEjUDXYZnReoRVg@public.gmane.org> wrote: >> 2009/6/12 Tyler Knappe <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: >> > Does anyone know how to dynamically generate div tags? >> >> > I tried this: >> >> > <th><div id = "test_div_" + ij"" style = "display :inline;"> Available >> > </div></th> >> >> > Which didn''t work, rather it gives me this in the source: >> >> > <th><div id = "test_div_" + ij"" style = "display :inline;"> >> > Available </div></th> >> >> Assuming you are in erb then <div id="test_div_#{i}#{j}" > or similar >> should work > > I think you meant <%= i%> rather than #{i} >@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
Frederick Cheung wrote:> On Jun 12, 4:31�pm, Colin Law <colinandyvo...-PMmbdagmEjUDXYZnReoRVg@public.gmane.org> wrote: >> > � � � �<th><div id = "test_div_" + ij"" style = "display :inline;"> >> > Available </div></th> >> >> Assuming you are in erb then <div id="test_div_#{i}#{j}" > or similar >> should work > > I think you meant <%= i%> rather than #{i} > > FredThis is the method I used. Thanks guys! -- Posted via http://www.ruby-forum.com/.