SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Nov 2001
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Instance variable not available in the view after adding sessions

    In my controller I had this working:
    Code:
    def index
      @channels = Channel.find(:all, :conditions => "active='1'", :order => "name asc")
    end
    And then it's used in the view by calling a helper
    Code:
    <%= create_directory (@channels) %>
    All of which worked fine until I started storing a few reused values in a session variable. I moved a few things around to get the sessions working properly, most notably ApplicationController executes a render to run the index action like this:
    Code:
    render :controller => "region", :action => "index"
    Which I don't think should cause this problem. Bascially, ApplicationController decides one of two different actions to call on the region controller. The other is a view that doesn't use any instance variables (static HTML for now). When I try to use an instance variable in the index view I get an error saying they're nil objects.

    Error is below:
    Code:
    NoMethodError in Region#index 
     Showing app/views/region/index.rhtml where line #13 raised: 
    You have a nil object when you didn't expect it!
    You might have expected an instance of Array.
    The error occured while evaluating nil.each
    Extracted source (around line #13): 
    10: 
    11: -->
    12: 
    13: <%= create_directory (@channels) %>
    RAILS_ROOT: /www/seven01/public/../config/..
    Application Trace | Framework Trace | Full Trace 
    #{RAILS_ROOT}/app/helpers/region_helper.rb:4:in `create_directory'
    #{RAILS_ROOT}/app/views/region/index.rhtml:13:in `_run_rhtml_region_index'
    #{RAILS_ROOT}/app/controllers/application.rb:21:in `get_city'
    Any ideas?

    Thanks.

  2. #2
    SitePoint Addict
    Join Date
    Nov 2001
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Crap... it's not the view that's the problem it's the helper. Still, can anybody tell me why I can't use that object in the helper when it's clearly passed in?

    The controller is called Region and the action is called index. The helper is in the RegionHelper.

    view:
    Code:
    <%= create_directory (@channels) %>
    helper:
    Code:
    def create_directory (channels)
    	channels.length.to_s
    end
    Even when I just try to pring the length of the array (or do anything at all with the channels variable inside the helper) I get the nil object error. If I change that helper to only output a string like "hello world" then it sucessfully executes.

    Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •