Greetings, I''m new to rails and currently having couple problems about how to add javascript functionality into rails. I have a simple method in the helper and i want to run this code on button click. ------------------------------------------------------------------ module PagesHelper def show_message "simple output" end end ------------------------------------------------------------------ this doesnt work <%= button_to_function "Print", "show_message()" %> this doesnt work too <input onclick=" show_message() " type="button" value="PRINT" /> Can someone please show to properly solve this problem? I would really apreciate it! thank you! -- 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 To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d37ed6e00e3e054c75147df57dabfed6%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
The `button_to_function` method''s second argument should be JavaScript, not Ruby. See: http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-button_to_function On Sunday, October 27, 2013 10:27:29 AM UTC-6, Ruby-Forum.com User wrote:> > Greetings, > I''m new to rails and currently having couple problems about how to add > javascript functionality into rails. > > I have a simple method in the helper and i want to run this code on > button click. > > ------------------------------------------------------------------ > module PagesHelper > def show_message > "simple output" > end > end > > ------------------------------------------------------------------ > > this doesnt work > <%= button_to_function "Print", "show_message()" %> > > this doesnt work too > <input onclick=" show_message() " type="button" value="PRINT" /> > > Can someone please show to properly solve this problem? I would really > apreciate it! thank you! > > -- > 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 To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/41a92d2b-1941-497a-a64d-18b7018bde80%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Kashif Umair Liaqat
2013-Oct-28 05:44 UTC
Re: run simple method from Helper on button click
You cannot call Ruby code directly from client side. You have to send an AJAX request to some controller for this purpose. On Sunday, October 27, 2013 9:27:29 PM UTC+5, Ruby-Forum.com User wrote:> > Greetings, > I''m new to rails and currently having couple problems about how to add > javascript functionality into rails. > > I have a simple method in the helper and i want to run this code on > button click. > > ------------------------------------------------------------------ > module PagesHelper > def show_message > "simple output" > end > end > > ------------------------------------------------------------------ > > this doesnt work > <%= button_to_function "Print", "show_message()" %> > > this doesnt work too > <input onclick=" show_message() " type="button" value="PRINT" /> > > Can someone please show to properly solve this problem? I would really > apreciate it! thank you! > > -- > 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 To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6667ccb0-d1fc-4a8c-a96d-819e4020fc45%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Kashif Umair Liaqat wrote in post #1125837:> You cannot call Ruby code directly from client side. You have to send an > AJAX request to some controller for this purpose.How would I do that? I have no knowledge of AJAX -- 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 To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3547ca542fdfc2183de2adfe073169dd%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Kashif Umair Liaqat
2013-Oct-29 06:28 UTC
Re: run simple method from Helper on button click
Suppose you have a controller HomeController. It should look like below. home_controller.rb class HomeController < ApplicationController # GET /show_message def show_message format.js { render js: "alert(''simple output'');" } endend and in your routs.rb file add this line get ''/show_message'', ''home#show_message'', as: :show_message and in your view add this line <%= link_to "Print", show_message_path, remote: true %> After this when you click on Print link, and AJAX request will be sent to HomeController''sshow_message action and it will display a javascript alert. For better understanding of AJAX requests in Rails, follow this tutorial - http://www.tutorialspoint.com/ruby-on-rails/rails-and-ajax.htm :) On Monday, October 28, 2013 7:52:06 PM UTC+5, Ruby-Forum.com User wrote:> > Kashif Umair Liaqat wrote in post #1125837: > > You cannot call Ruby code directly from client side. You have to send an > > AJAX request to some controller for this purpose. > > How would I do that? I have no knowledge of AJAX > > -- > 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 To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/109ac5c3-c5b7-452d-bdd0-461047aa960a%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Thanks a lot! -- 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 To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9ec245b26f8c8255d200ed261c81e3c6%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.