3 Replies - 4080 Views - Last Post: 25 December 2010 - 01:35 AM Rate Topic: -----

#1 german129  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 43
  • Joined: 03-September 10

Rails: variable initialization and scope

Posted 20 December 2010 - 04:27 PM

I am having this really strange problem and its mainly because i dont understand how rails works. I have an auction controller with an auction view. As i understand, any class variable (starts with an @ sign) should be available in my view. I am instantiating a class variable in my index method of my controller and the variable is available in that view. However, any time i try and use that variable again later in a partial it is not longer instantiated. Does rails create a new instance of the auction controller every time there is a request? if this is the case, how do i store data if i am not using a database??

sample code:

class AuctionController < ApplicationController

def index
  @game_type = 1
end

def start_game
  render :update do |page|
    page.replace :start_button, :partial => "start_button"
  end
end
end

_start_button.html.erb:

<% if @game_type == 1 %>
  <%= button_to_function "start", remote_function(function1) %>
<% else %>
  <%= button_to_function "start", remote_function(function2) %>
<% end %>




So i am assigning a value of 1 to the variable game_type once the index method is called. then once the auctioneer implements the start_game method it should render the start_button with a function depending on teh @game_type variable. For some reason once start_game is called the @game_type variable is nil again, after it had been assigned a number.

This post has been edited by german129: 20 December 2010 - 04:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Rails: variable initialization and scope

#2 The Neoracle  Icon User is offline

  • Check, check, 1, 2.
  • member icon

Reputation: 21
  • View blog
  • Posts: 4,097
  • Joined: 30-March 01

Re: Rails: variable initialization and scope

Posted 20 December 2010 - 04:34 PM

Yes, Rails is going to use a different instance of the controller for every request. You cannot store information request to request using instance variables (@game_type is an instance variable).

You must use some sort of data store to persist data between requests. You could store it in a database, or a client side cookie.
Was This Post Helpful? 1
  • +
  • -

#3 german129  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 43
  • Joined: 03-September 10

Re: Rails: variable initialization and scope

Posted 20 December 2010 - 04:57 PM

Wow that blows my mind. I was planning to keep the entire program in memory so i did not need a db but this makes that nearly impossible. I guess my only option is to use global variables, which i always hear is a frowned upon for object oriented programming. From a performance standpoint, is there a significant difference between storing your data in a db and storing them in memory (global variables)??
Was This Post Helpful? 0
  • +
  • -

#4 MitkOK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 32
  • View blog
  • Posts: 403
  • Joined: 09-August 07

Re: Rails: variable initialization and scope

Posted 25 December 2010 - 01:35 AM

Dude, learn some Ruby before jumping into Rails.

PS: Don't use global vars, it's really bad idea, do some search to find out why.
You can store data using session - http://guides.rubyon...ew.html#session

This post has been edited by MitkOK: 25 December 2010 - 01:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1