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


#1

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

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.


#2

Vikas G. 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

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


#3

Stefan R. 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)