Forum: Ruby How to change ruby's global variable through onclick event?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3832c46fac01379dfa5b14c60fac8b5c?d=identicon&s=25 Vikas Gholap (vikas)
on 2009-01-22 07:39
Hello all,

I have problem that how to change ruby's global variable through onclick
event.?

my code is like in myvideo.html.erb file.
--------------------------------------------------------------------------
<a id='more_videos_link' class='ActionLink' href=\"#\"
onclick=\"something that change variable\">More videos</a>
--------------------------------------------------------------------
through onclick how can i change variable declared in my_controller.rb

-------------------------------------------------------
class myController < ApplicationController
$moreVideo = false

def moreVideo
    $moreVideo = true
    respond_to do |format|
      format.html { render_partial 'home/video_search' }
    end

  end

end
--------------------------------------------------------------

is there way to change $moreVideo=true through onclick or href of anchor
tag given above.
Cf7cd97cdc8ed7d4ae92965b24f0dfad?d=identicon&s=25 Stefan Rusterholz (apeiros)
on 2009-01-22 10:45
Vikas Gholap wrote:
> Hello all,
>
> I have problem that how to change ruby's global variable through onclick
> event.?
>
> my code is like in myvideo.html.erb file.
> --------------------------------------------------------------------------
> <a id='more_videos_link' class='ActionLink' href=\"#\"
> onclick=\"something that change variable\">More videos</a>
> --------------------------------------------------------------------
> through onclick how can i change variable declared in my_controller.rb
>
> -------------------------------------------------------
> class myController < ApplicationController
> $moreVideo = false
>
> def moreVideo
>     $moreVideo = true
>     respond_to do |format|
>       format.html { render_partial 'home/video_search' }
>     end
>
>   end
>
> end
> --------------------------------------------------------------
>
> is there way to change $moreVideo=true through onclick or href of anchor
> tag given above.

Hi

You're probably better off in the rubyonrails mailing list instead of
the ruby ML.
However, a short answer anyway: to change values on the server side
(that's where your ruby code is) upon client side events (where there's
no executed ruby code anymore) you'll need a client side code such as
javascript e.g. using ajax to communicate that event to the server side.
On another note: you do NOT want a global variable there. A global will
be used not just for that single user but for all happening to be served
by the same instance of ruby.

Regards
Stefan
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-22 11:04
Stefan Rusterholz wrote:
> On another note: you do NOT want a global variable there. A global will
> be used not just for that single user but for all happening to be served
> by the same instance of ruby.

And in most Rails deployments, there are multiple processes, so a global
variable changed in one will not affect another.

You probably want to change state in the *session*, which is accessible
to any process handling data on behalf of the same user.

  session[:foo] = "bar"

(By default the session is stored in a cookie, so the total size is
limited to 4K. If you need to store more than this, select a different
session store model)
This topic is locked and can not be replied to.