I have a situation that I am not sure how to approach. I have a global variable @date which gets loaded in an ajax action: def calendar_ajax @date = Time.parse("#{params[:date]} || Time.now.utc") This @date is used to find events for the calendar. A partial is then rendered: render :update do |page| page.replace_html "internal_calendar_box", :partial => ''calendar_large_data'' end I then have javascript functions which are within tabs and triggered by an onclick that then create an ajax request in order to populate the calendar with a different kind of request. The problem I am facing is that the javascript function needs to send the current @date variable, which changes upon the change of a month, but the original @date object is always being passed. I think the problem lies in the fact that this javascript function does not see the new @date objects that are being created when the month is changed and a new partial is created. I tried sticking these functions within the partial itself, but that does not seem to work. Anyone have any ideas? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Feb-18 08:28 UTC
Re: Pass a global variable from a partial from javascript
On Feb 18, 3:12 am, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a situation that I am not sure how to approach. I have a > global variable @date which gets loaded in an ajax action: > > def calendar_ajax > @date = Time.parse("#{params[:date]} || Time.now.utc") > > This @date is used to find events for the calendar. > A partial is then rendered: > > render :update do |page| > page.replace_html "internal_calendar_box", :partial => > ''calendar_large_data'' > end > > I then have javascript functions which are within tabs and triggered > by an onclick that then create an ajax request in order to populate > the calendar with a different kind of request. The problem I am > facing is that the javascript function needs to send the current @date > variable, which changes upon the change of a month, but the original > @date object is always being passed. I think the problem lies in the > fact that this javascript function does not see the new @date objects > that are being created when the month is changed and a new partial is > created. I tried sticking these functions within the partial itself, > but that does not seem to work. Anyone have any ideas?I;m not entirely sure what is going on (eg how does the original date object get set in javascript), but if you are expecting your javascript to magically react to the change of a controller instance variable that isn''t going to happen. Depending on what it is that is going on you may find that adding page.assign(''some_variable'', ''some_value'') to your render :update block does the trick. Fred --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---
I am not quite sure I am following, but I understand that after updating your calendar using the method you provided you are trying to submit the current date back to the server whenever a user clicks to change the month? To understand what''s going what''s going on I would have to see your calendar partial and how you are trying to submit the date. For now I can only say that of course javascript can not access a ruby variable directly. You can use ruby/erb to generate javascript and thus indirectly pass ruby variables. On Feb 18, 4:12 am, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a situation that I am not sure how to approach. I have a > global variable @date which gets loaded in an ajax action: > > def calendar_ajax > @date = Time.parse("#{params[:date]} || Time.now.utc") > > This @date is used to find events for the calendar. > A partial is then rendered: > > render :update do |page| > page.replace_html "internal_calendar_box", :partial => > ''calendar_large_data'' > end > > I then have javascript functions which are within tabs and triggered > by an onclick that then create an ajax request in order to populate > the calendar with a different kind of request. The problem I am > facing is that the javascript function needs to send the current @date > variable, which changes upon the change of a month, but the original > @date object is always being passed. I think the problem lies in the > fact that this javascript function does not see the new @date objects > that are being created when the month is changed and a new partial is > created. I tried sticking these functions within the partial itself, > but that does not seem to work. Anyone have any ideas?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---