Hello all, I am using a jquery plugin to search content on a page and highlight keywords. The function call looks like this: <script type="text/javascript"> $(''div'').highlight(''@found''); </script> I want the user to be able to assign the variable @found through an input field. It seems like it should be fairly easy but I am new to rails and have difficulty trying to make this work. So far I have tried to create a method in the controller such as: def found @found = "" #I''ve also tried @found = gets.chomp but that looks funny in rails end and then create the input field like such: <%= form_tag do %> <%= text_field :found, :found %> <%= submit_tag("Post") %> <% end %> The page looks fine but it does not assign the variable. If I try <%= form_tag(@found) do %> <%= f.text_field :found %> <%= f.submit_tag("Post") %> <% end %> I get errors. I may be going about this the wrong way. Any advice would be great. Thanks -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Apr 25, 2013, at 11:20 AM, Brentwood R. wrote:> I may be going about this the wrong way. Any advice would be great.You are possibly confused on more than 1 issue, but there''s not enough info in your message to be sure or to let anyone provide a really complete answer. (In particular, do you expect the user to enter the value and then be able to perform the search all in the page, or is the user submitting a form back to the server and a new page being rendered?) That said: 1) The ruby code in the rails controller runs just before render, and affects the page that is sent to the browser. So the controller might change the javascript that''s in the page as it''s sent, but no ruby code will be run and no ruby variables will be evaluated after it''s sent. 2) highlight(''@found''); will pass the literal string @found to the highlight function, if you want to substitute a ruby variable value in there, you need highlight(''<%= @found %>'')--assuming of course that @found has its value while the page is being rendered 3) What do you mean by "does not assign the variable"? What ruby will get in the post is a hash of params, and it''s up to you how you use that. In the most common use, you''d use a form with a model object, but here you''re just trying to get 1 value, so use of form_tag is probably appropriate, and in that case you want text_field_tag, not text_field. -- Scott Ribe scott_ribe-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org http://www.elevated-dev.com/ (303) 722-0567 voice -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Scott, Thanks for your response. I''ll try to be more precise. The way the jquery plugin work is that if I place a keyword in the function call like so: <script type="text/javascript"> $(''div'').highlight(''something''); </script> ...all instances of "something" will be highlighted in the ''div''. What I''m trying to do is make it so that a user can enter a keyword (through an input field) so that whatever keyword they entered would be highlighted on the page. I figured that what might work was to put some variable in the function and assign the value of the variable through the input field. -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Apr 25, 2013, at 4:41 PM, Brentwood R. wrote:> ...all instances of "something" will be highlighted in the ''div''. What > I''m trying to do is make it so that a user can enter a keyword (through > an input field) so that whatever keyword they entered would be > highlighted on the page.OK. This has nothing at all to do with ruby or rails. What you want is a javascript event handler on the input field which then calls that highlight function. You''d call something like: $(''div'').highlight($(''#my_input_field'').val()) How you arrange to call that varies, you could set up the event handler directly as an option in the text_field_tag call in the .html.erb template: <%= text_field_tag(:my_input_field, '''', {onchange: ''call_the_highlight_fun()''}) %> You could skip the embedded ruby altogether since you''re not really using rails in that little piece and just put in <form> and <input> tags. Or you could use "unobtrusive javascript", which I prefer: leave the input field alone, and in a document loaded handler, add the event handler to the input field: $(function() {$(''#my_input_field'').change(function() {call_the_highlight_fun()})}) Note that: - I''m whipping this off the top of my head, not all syntax may be correct, but you seem to be looking for help with the general approach. - I''m assuming jquery. - I''m assuming recent rails and ruby 1.9 (thus the {symbol: value} syntax for a hash). -- Scott Ribe scott_ribe-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org http://www.elevated-dev.com/ (303) 722-0567 voice -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.