Mischa Berger
2006-May-18 20:12 UTC
[Rails] access to an ApplicationController property from a view
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 Walton
2006-May-18 20:41 UTC
[Rails] access to an ApplicationController property from a view
Hi Mischa, Mischa Berger 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 Berger
2006-May-18 20:54 UTC
[Rails] Re: access to an ApplicationController property from a view
> 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, > BillThanks again.
Gregory Seidman
2006-May-18 21:02 UTC
[Rails] access to an ApplicationController property from a view
On Thu, May 18, 2006 at 10:11:29PM +0200, Mischa Berger 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