Max Williams
2010-May-24 09:16 UTC
Getting js variable value or form field value in js.rjs file
Hi all I have a file "index.js.rjs" where i only want to run the code in it (which updates the contents of a div in typical ajax style) if a variable i have from params is the same as a value of one of the form fields on the page (which has the id ''search_timestamp''). I can''t work out, though, how to get that value in my js.rjs file. Something like this if @timestamp.to_s == page[''search_timestamp''].val() #my code end I feel like this should be easy but can''t figure it out. Can anyone show me how? I''ve tried just using page.call with some jquery but again can''t work out how to get the resultant value back into my ruby code. I''m using jquery and jrails in my app in case that''s relevant. thanks, max -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-May-24 13:53 UTC
Re: Getting js variable value or form field value in js.rjs file
On May 24, 10:16 am, Max Williams <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi all > > I have a file "index.js.rjs" where i only want to run the code in it > (which updates the contents of a div in typical ajax style) if a > variable i have from params is the same as a value of one of the form > fields on the page (which has the id ''search_timestamp''). I can''t work > out, though, how to get that value in my js.rjs file. Something like > this > > if @timestamp.to_s == page[''search_timestamp''].val() > #my code > end > > I feel like this should be easy but can''t figure it out. Can anyone > show me how? I''ve tried just using page.call with some jquery but again > can''t work out how to get the resultant value back into my ruby code. > I''m using jquery and jrails in my app in case that''s relevant. >At the point that your rjs file is run you can''t get the value of anything on user''s page, because it''s running on your server, not the user''s browser. I wrote a little about this a while ago at http://www.spacevatican.org/2008/5/26/conditional-rjs-explained Fred> thanks, max > -- > Posted viahttp://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Max Williams
2010-May-24 14:33 UTC
Re: Getting js variable value or form field value in js.rjs file
Frederick Cheung wrote:> On May 24, 10:16�am, Max Williams <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> � #my code >> end >> >> I feel like this should be easy but can''t figure it out. �Can anyone >> show me how? �I''ve tried just using page.call with some jquery but again >> can''t work out how to get the resultant value back into my ruby code. >> I''m using jquery and jrails in my app in case that''s relevant. >> > > At the point that your rjs file is run you can''t get the value of > anything on user''s page, because it''s running on your server, not the > user''s browser. I wrote a little about this a while ago at > http://www.spacevatican.org/2008/5/26/conditional-rjs-explained > > FredHi Fred - thanks, i should have known that. Looking at your great blog post, i adapted this approach: page << "if($(''some_div'').visible()){" page[''some_div''].replace :partial => ''some_partial'' page << ''}else{'' page.insert_html :after, ''something_else'', :partial => ''header'' page << ''}'' like so: page << "if($(''#search_timestamp'').val() == #{params[:timestamp]}){" page.replace_html "questions", :partial => "questions/questions" page.replace "flashes", :partial => "common/flashes" page << "}" and that''s worked a treat. Obviously it means i''m still generating the html from the partials every time, whether use it or not, but i think that''s the best that can be done here. thanks agaain, max -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.