Hi all, I have quite a lot of methods packed into a controller which all share some common objects. Instead of defining them in each method I want to define them all in class variables like this: old: ----------------------------------------------------- class MainController < ... def first_m @categories = Category.find_all, some_conditions ... end def second_m @categories = Category.find_all, some_conditions ... [ more variable assignments ] render_action :first_m end def third_m @categories = Category.find_all, some_conditions ... [ more variable assignments ] render_action :first_m end end ------------------------------------------------------ new: ----------------------------------------------------- class MainController < ... @@categories = Category.find_all, some_conditions ... def first_m end def second_m [ more variable assignments ] render_action :first_m end def third_m [ more variable assignments ] render_action :first_m end end ----------------------------------------------------- In the view: ----------------------------------------------------- <% @@categories.each do |@category| %> ... <% end %> ----------------------------------------------------- I do however get a uninitialized class variable @@categories in ActionView::Base error. Any ideas on how to define a class variable so that it is available to all the methods? I could perhaps do it by defining a get_categories method, and then calling it in all methods where I need it, I guess. Any suggestions? Many thanks in advance, Nicky
The view does not have access to class variables (@@). Try doing
something more like:
class MainController < ApplicationController
before_filter :get_categories
def get_categories
@categories = Category.find_all, some_conditions ...
end
end
-Lucas
http://www.rufy.com/
On Apr 6, 2005, at 12:26 AM, Nickolay Kolev wrote:
> Hi all,
>
> I have quite a lot of methods packed into a controller which all share
> some common objects. Instead of defining them in each method I want to
> define them all in class variables like this:
>
> old:
> -----------------------------------------------------
> class MainController < ...
>
> def first_m
> @categories = Category.find_all, some_conditions ...
> end
>
> def second_m
> @categories = Category.find_all, some_conditions ...
> [ more variable assignments ]
> render_action :first_m
> end
>
> def third_m
> @categories = Category.find_all, some_conditions ...
> [ more variable assignments ]
> render_action :first_m
> end
>
> end
> ------------------------------------------------------
>
> new:
> -----------------------------------------------------
> class MainController < ...
>
> @@categories = Category.find_all, some_conditions ...
>
> def first_m
> end
>
> def second_m
> [ more variable assignments ]
> render_action :first_m
> end
>
> def third_m
> [ more variable assignments ]
> render_action :first_m
> end
>
> end
> -----------------------------------------------------
>
> In the view:
> -----------------------------------------------------
> <% @@categories.each do |@category| %>
> ...
> <% end %>
> -----------------------------------------------------
>
>
> I do however get a
> uninitialized class variable @@categories in ActionView::Base
> error.
>
> Any ideas on how to define a class variable so that it is available to
> all the methods?
>
> I could perhaps do it by defining a get_categories method, and then
> calling it in all methods where I need it, I guess.
>
> Any suggestions?
>
> Many thanks in advance,
> Nicky
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
> class MainController < ApplicationController > before_filter :get_categories > > def get_categories > @categories = Category.find_all, some_conditions ... > end > endThanks Lucas! Just what I needed... Nicky
Is that the same as:
class MainController < ApplicationController
before_filter :get_categories
@categories
def get_categories
@categories = Category.find_all, some_conditions ...
end
end
I know my example above works, but is it exactly the same in practice
as the example quoted below? In my example above, I know that
@categories can be accessed by any method, would this hold true for
when @categories is defined/first mentioned in a method too?
... I need a good book on Ruby and a free weekend :)
Thanks,
Ben
On Apr 6, 2005 7:03 PM, Nickolay Kolev
<nmkolev-OhoefBWHl6Eb1SvskN2V4Q@public.gmane.org>
wrote:> > class MainController < ApplicationController
> > before_filter :get_categories
> >
> > def get_categories
> > @categories = Category.find_all, some_conditions ...
> > end
> > end
>
> Thanks Lucas! Just what I needed...
>
> Nicky
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
Is is unnecessary to say @categories in the middle of the class definition like that. It doesn''t hurt anything but it doesn''t add anything and makes the code more confusing. -Lucas http://www.rufy.com/ On Apr 6, 2005, at 6:45 AM, Ben Myles wrote:> Is that the same as: > > class MainController < ApplicationController > before_filter :get_categories > > @categories > > def get_categories > @categories = Category.find_all, some_conditions ... > end > end > > I know my example above works, but is it exactly the same in practice > as the example quoted below? In my example above, I know that > @categories can be accessed by any method, would this hold true for > when @categories is defined/first mentioned in a method too? > > ... I need a good book on Ruby and a free weekend :) > > Thanks, > Ben > > On Apr 6, 2005 7:03 PM, Nickolay Kolev <nmkolev-OhoefBWHl6Eb1SvskN2V4Q@public.gmane.org> wrote: >>> class MainController < ApplicationController >>> before_filter :get_categories >>> >>> def get_categories >>> @categories = Category.find_all, some_conditions ... >>> end >>> end >> >> Thanks Lucas! Just what I needed... >> >> Nicky >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Wed, Apr 06, 2005 at 12:33:30AM -0700, Lucas Carlson wrote:> The view does not have access to class variables (@@). Try doing > something more like: > > class MainController < ApplicationController > before_filter :get_categories > > def get_categories > @categories = Category.find_all, some_conditions ... > end > endContinuing with that example, would it be possible to have in the main layout a call to a viewer of the categories. For example, if we want to show a list of categories in our main application screen i think its better to call the list method defined por categories than write again the same method for the MainController. Is it clear what i mean? Thanks! -- Fernando Blat Peris <ferblape-I8oKXW6Z+bze5aOfsHch1g@public.gmane.org>