Forum: Ruby on Rails Global variables and assigning the values

82924a47a90d69f2885a657e7a6be111?d=identicon&s=25 Robert Negronida (gman327)
on 2012-11-18 09:20
I am having an issue where the global variable, $r, which i declared in
the controller is being overwritten by my code which is in a view:

<%= link_to 'Picks test1', game_pick_page_path, :onclick => $r = 1 %>
<%= link_to 'Picks test2', game_pick_page_path, :onclick => $r = 2 %>

The first $r = 1 is overwritten by $r = 2 making the value 2 even if i
click picks test1. i want it to just set $r to which every value is
associated with the link the user clicks.

Any ideas on how to make this happen would be greatly appreciated.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2012-11-18 09:49
(Received via mailing list)
On Nov 18, 8:21am, Robert Negronida <li...@ruby-forum.com> wrote:
> I am having an issue where the global variable, $r, which i declared in
> the controller is being overwritten by my code which is in a view:
>
> <%= link_to 'Picks test1', game_pick_page_path, :onclick => $r = 1 %>
> <%= link_to 'Picks test2', game_pick_page_path, :onclick => $r = 2 %>
>
> The first $r = 1 is overwritten by $r = 2 making the value 2 even if i
> click picks test1. i want it to just set $r to which every value is
> associated with the link the user clicks.
>

I'm not sure what you trying to do. If you don't want the global to be
changed, why are you assigning to it? Legitimate uses are few and far
between, I'm sure an alternative can be suggested if you can clarify
your intent.

Fred.
82924a47a90d69f2885a657e7a6be111?d=identicon&s=25 Robert Negronida (gman327)
on 2012-11-18 10:04
Fred,
When the user clicks the 'picks1' link I want $r to be equal to 1. when
the user clicks 'picks2' i want $r to be equal to 2. I plan to us these
values that are assigned to $r in the page that these links bring the
user to. At this moment with the code i have presented earlier i
constantly get $r equal to 2 whether i click the second link or not.
What is happening is that the code is being read over and actually
changing $r instead of just doing so when the user clicks the links with
the onclick. I need to figure a way to
change the $r so that it will change based on the link the user
clicks... i already had it set up with a model and it worked but i soon
realized that the amount of data entries was not ideal so i wanted to
figure out how to do it with a global variable.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2012-11-18 13:53
(Received via mailing list)
On Nov 18, 9:04am, Robert Negronida <li...@ruby-forum.com> wrote:
> When the user clicks the 'picks1' link I want $r to be equal to 1. when
> the user clicks 'picks2' i wont $r to be equal to 2. At this moment with
> the code i have presented earlier i constantly get $r equal to 2 whether
> i click the second link or not. What is happening is that the code is
> being read over and actually changing $r instead of just doing so when
> the user clicks the links with the onclick. I need to figure a way to
> change the $r so that it will change based on the link the user
> clicks... i already had it set up with a model and it worked but i soon
> realized that the amount of data entries was not ideal.

Sounds like you want to add this as a parameter to the action, for
example game_pick_page_path(:test => 1) which will cause params[:test]
to be set to 1. If you want to remember the value without storing it
in the database, use the session (set session[:foo]) rather than a
global - a global would share its value across all users of the site
and won't work consistently if you have more than 1 passenger/unicorns
instance

Fred
82924a47a90d69f2885a657e7a6be111?d=identicon&s=25 Robert Negronida (gman327)
on 2012-11-18 20:03
> Sounds like you want to add this as a parameter to the action, for
> example game_pick_page_path(:test => 1) which will cause params[:test]
> to be set to 1. If you want to remember the value without storing it
> in the database, use the session (set session[:foo]) rather than a
> global - a global would share its value across all users of the site
> and won't work consistently if you have more than 1 passenger/unicorns
> instance
>
> Fred

That makes alot of sense... but how do i use the (set session[:foo]).
How do i define it in the controller and view?
15eb13d662803f57bc4aea59704988b4?d=identicon&s=25 Scott Ribe (Guest)
on 2012-11-18 20:13
(Received via mailing list)
On Nov 18, 2012, at 12:03 PM, Robert Negronida wrote:

> That makes alot of sense... but how do i use the (set session[:foo]).
> How do i define it in the controller and view?

session[:foo] = ...

--
Scott Ribe
scott_ribe@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.