Forum: Ruby on Rails access to an ApplicationController property from a view

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.
Mischa B. (Guest)
on 2006-05-19 00:13
(Received via mailing list)
Hi there,

How is it possible to get access to an ApplicationController property
from a view?

Consider this (simplified) situation:

class ApplicationController < ActionController::Base
   def folder_id
     @folder_id = params[:id]
     return @folder_id
   end
end

class FolderController < ApplicationController
   def show
     @folder = Folder.find_by_id(folder_id)
   end
end

So far, so good, BUT... when trying to access folder_id in the show-view
like this <%= folder_id %>, I get an error (undefined local variable or
method `folder_id'). Is this possible or do I NEED to use instance
variables -- which I'd rather not do?

Thanks.

Mischa.

--
Open source file management webapp :: Boxroom ::
http://boxroom.rubyforge.org/
Bill W. (Guest)
on 2006-05-19 00:42
(Received via mailing list)
Hi Mischa,

Mischa B. wrote:

> So far, so good, BUT... when trying to access folder_id in the show-view
> like this <%= folder_id %>, I get an error (undefined local variable or
> method `folder_id'). Is this possible or do I NEED to use instance
> variables -- which I'd rather not do?

You'll need to use instance variables.  By definition, the scope of a
local
variable is the block within which it's declared.  A local variable
declared
in a controller action method only has meaning within that action
method.
Not only is it not available in a view, it's not available in any of the
other action methods in the controller.

Just as a point of information, what's your issue with using instance
variables?

Best regards,
Bill
Mischa B. (Guest)
on 2006-05-19 00:56
(Received via mailing list)
> You'll need to use instance variables.  By definition, the scope of a
> local variable is the block within which it's declared.  A local
> variable declared in a controller action method only has meaning within
> that action method. Not only is it not available in a view, it's not
> available in any of the other action methods in the controller.

Yeah, but I'm not trying to use a local variable, I'm trying to access
the method folder_id which returns @folder_id.

Why IS it possible to use params[:id] instead of @params[:id] in a view
and why ISN'T it possible to use folder_id instead of @folder_id like
I'm trying to do in my example?

> Just as a point of information, what's your issue with using instance
> variables?

I'm trying to encapsulate @folder_id. The view only has to know about
folder_id. In the method I can set @folder_id to different values
according to the situation and return the appropriate value.

> Best regards,
> Bill

Thanks again.
Gregory S. (Guest)
on 2006-05-19 01:05
(Received via mailing list)
On Thu, May 18, 2006 at 10:11:29PM +0200, Mischa B. wrote:
} Hi there,
}
} How is it possible to get access to an ApplicationController property
} from a view?
}
} Consider this (simplified) situation:
}
} class ApplicationController < ActionController::Base
}   def folder_id
}     @folder_id = params[:id]
}     return @folder_id
}   end
} end
}
} class FolderController < ApplicationController
}   def show
}     @folder = Folder.find_by_id(folder_id)
}   end
} end
}
} So far, so good, BUT... when trying to access folder_id in the
show-view
} like this <%= folder_id %>, I get an error (undefined local variable
or
} method `folder_id'). Is this possible or do I NEED to use instance
} variables -- which I'd rather not do?

<%= controller.folder_id %>

} Thanks.
} Mischa.
--Greg
This topic is locked and can not be replied to.