FYI, I''m new to Ruby, so use kid gloves with me. Given a parent class with a public method: class ApplicationController < ActionController::Base def show_left_column (bool) @show_left_column = bool end end Shouldn''t I be able to call the show_left_column method from a subclass as such: class UserController < ApplicationController model :user scaffold :user show_left_column true end I keep getting errors indicating the method cannot be found, but as far I as I can tell, what I am doing is no different than the model and scaffold methods. Can anybody correct my ignorance, please?! -Theo _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
show_left_column is an instance method, which requires an instance of ApplicationController or subclasses. On 10/14/05, Theodore Mills <twmills-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> FYI, I''m new to Ruby, so use kid gloves with me. > > Given a parent class with a public method: > > class ApplicationController < ActionController::Base > def show_left_column (bool) > @show_left_column = bool > end > end > > Shouldn''t I be able to call the show_left_column method from a subclass as > such: > > class UserController < ApplicationController > model :user > scaffold :user > show_left_column true > end > > I keep getting errors indicating the method cannot be found, but as far I > as I can tell, what I am doing is no different than the model and scaffold > methods. > > Can anybody correct my ignorance, please?! > > -Theo > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
On 10/14/05, Theodore Mills <twmills-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I keep getting errors indicating the method cannot be found, but as far I > as I can tell, what I am doing is no different than the model and scaffold > methods.What you have defined in your ApplicationController is an instance method, and what you are calling in your UserController is a class method, so the errors are correct. But your problems run slightly deeper than this because, if you simply change your method definition to: class ApplicationController < ActionController::Base def self.show_left_column (bool) @show_left_column = bool end end Note the ''self'' bit on the method definition, which defines it as a class method. Then this will likely complain too because, in Ruby, @show_left_column is a declaration of an instance variable, meaning that each instance of ApplicationController will have it''s own copy (and therefore could be different from any other). If you change the code to: class ApplicationController < ActionController::Base def self.show_left_column (bool) @@show_left_column = bool end end Then @@show_left_column is a class variable meaning that all instances of the ApplicationController will share this value, so it is not different for each instance. I probably haven''t explained that at all well but, if you don''t have it, I highly recommend the Programming Ruby book (http://www.pragmaticprogrammer.com/titles/ruby/index.html) as it *will* teach you Ruby properly. There''s a copy of the 1st edition online (http://www.rubycentral.com/book/) but it''s for pre-1.8 versions of Ruby so is a little out-of-date. If you look at the "Class Variables and Class Methods" section on http://www.rubycentral.com/book/tut_classes.html you''ll get an idea of what I''m trying to say about your problem. Matt
> > FYI, I''m new to Ruby, so use kid gloves with me. > > Given a parent class with a public method: > > class ApplicationController < ActionController::Base > def show_left_column (bool) > @show_left_column = bool > end > end > > Shouldn''t I be able to call the show_left_column method from a subclass as > such: > > class UserController < ApplicationController > model :user > scaffold :user > show_left_column true > end > > I keep getting errors indicating the method cannot be found, but as far I > as I can tell, what I am doing is no different than the model and scaffold > methods. > > Can anybody correct my ignorance, please?!You''ve defind show_left_column as an instance method of ApplicationController but you are calling it in the class context in UserController. What you probably want is a class method assigning a value to a class variable: class ApplicationController < ActionController::Base def self.show_left_column(bool) @@show_left_column = bool end _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Nick, Now the method is getting found, thanks. However, that variable (whether instance or class) is not available to my view, for some reason. Any ideas? Thanks again! -Theo On 10/14/05, Nick Sieger <nicksieger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > FYI, I''m new to Ruby, so use kid gloves with me. > > > > Given a parent class with a public method: > > > > class ApplicationController < ActionController::Base > > def show_left_column (bool) > > @show_left_column = bool > > end > > end > > > > Shouldn''t I be able to call the show_left_column method from a subclass > > as such: > > > > class UserController < ApplicationController > > model :user > > scaffold :user > > show_left_column true > > end > > > > I keep getting errors indicating the method cannot be found, but as far > > I as I can tell, what I am doing is no different than the model and scaffold > > methods. > > > > Can anybody correct my ignorance, please?! > > > You''ve defind show_left_column as an instance method of > ApplicationController but you are calling it in the class context in > UserController. What you probably want is a class method assigning a value > to a class variable: > > class ApplicationController < ActionController::Base > def self.show_left_column(bool) > @@show_left_column = bool > end > > > > > _______________________________________________ > 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
Thanks a lot, Matt. I''ve gotten by the first hurdle, now my question seems to be more Rails-related. I had thought all instance variables in Controller were automatically propogated to the View, but that doesn''t seem to be the case. In this case, I want all subclasses to inherit certain properties without me having to redefine them in each subclass. For instance, I want all administrative pages (such as user) to display a left column. I read this: The view has access to the instance-variables of the controller. It''s a lot like they are friend classes in that the instance-data simply is available in the view templates, as if it were in the same scope. But that doesn''t seem to be true. So my questions is, how do I get the view to see my instance variables? Thanks! -Theo On 10/14/05, Matthew Denner <matt.denner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 10/14/05, Theodore Mills <twmills-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I keep getting errors indicating the method cannot be found, but as far > I > > as I can tell, what I am doing is no different than the model and > scaffold > > methods. > > What you have defined in your ApplicationController is an instance > method, and what you are calling in your UserController is a class > method, so the errors are correct. > > But your problems run slightly deeper than this because, if you simply > change your method definition to: > > class ApplicationController < ActionController::Base > def self.show_left_column (bool) > @show_left_column = bool > end > end > > Note the ''self'' bit on the method definition, which defines it as a > class method. > > Then this will likely complain too because, in Ruby, @show_left_column > is a declaration of an instance variable, meaning that each instance > of ApplicationController will have it''s own copy (and therefore could > be different from any other). > > If you change the code to: > > class ApplicationController < ActionController::Base > def self.show_left_column (bool) > @@show_left_column = bool > end > end > > Then @@show_left_column is a class variable meaning that all instances > of the ApplicationController will share this value, so it is not > different for each instance. > > I probably haven''t explained that at all well but, if you don''t have > it, I highly recommend the Programming Ruby book > (http://www.pragmaticprogrammer.com/titles/ruby/index.html) as it > *will* teach you Ruby properly. There''s a copy of the 1st edition > online (http://www.rubycentral.com/book/) but it''s for pre-1.8 > versions of Ruby so is a little out-of-date. If you look at the > "Class Variables and Class Methods" section on > http://www.rubycentral.com/book/tut_classes.html you''ll get an idea of > what I''m trying to say about your problem. > > Matt > _______________________________________________ > 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
> > > Now the method is getting found, thanks. > > However, that variable (whether instance or class) is not available to my > view, for some reason. Any ideas?You probably want an accessor method like class AplicationController < ActionController::Base # ... def self.show_left_column? @@show_left_column end end then in your view you would have <% if controller.class.show_left_column? %> ... left column here ... <% end %> HTH /Nick _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> > > Now the method is getting found, thanks. > > However, that variable (whether instance or class) is not available to my > view, for some reason. Any ideas?You probably want an accessor method like class AplicationController < ActionController::Base # ... def self.show_left_column? @@show_left_column end end then in your view you would have <% if controller.class.show_left_column? %> ... left column here ... <% end %> HTH /Nick _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> > > Now the method is getting found, thanks. > > However, that variable (whether instance or class) is not available to my > view, for some reason. Any ideas?You probably want an accessor method like class AplicationController < ActionController::Base # ... def self.show_left_column? @@show_left_column end end then in your view you would have <% if controller.class.show_left_column? %> ... left column here ... <% end %> HTH /Nick _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Kick ass, thanks for the tip! I didn''t realize there was a global controller variable available to the template. It works! On 10/14/05, Nick Sieger <nicksieger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > Now the method is getting found, thanks. > > > > However, that variable (whether instance or class) is not available to > > my view, for some reason. Any ideas? > > > > You probably want an accessor method like > > class AplicationController < ActionController::Base > # ... > def self.show_left_column? > @@show_left_column > end > end > > then in your view you would have > > <% if controller.class.show_left_column? %> > ... left column here ... > <% end %> > > HTH > /Nick > > > > > _______________________________________________ > 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
Matt-
The instance variables that your view has automatic access to
have to be in the action in your controller. Like so:
class MyController < ApplicationController
@another_instance_var_not_in_the_action_that_you_are_calling
def index
@left_column = LeftColumn.find :all, :conditions => "whatever =
something"
@posts = Post.find(params[:id}
@categories = Category.find :all
end
end
So in your index.rhtml view file you will have access to:
@left_column, @posts and @categories. But you won''t have access to :
@another_instance_var_not_in_the_action_that_you_are_calling
HTH-
-Ezra
On Oct 14, 2005, at 12:12 PM, Theodore Mills wrote:
> Thanks a lot, Matt.
>
> I''ve gotten by the first hurdle, now my question seems to be more
> Rails-related.
>
> I had thought all instance variables in Controller were
> automatically propogated to the View, but that doesn''t seem to be
> the case. In this case, I want all subclasses to inherit certain
> properties without me having to redefine them in each subclass.
> For instance, I want all administrative pages (such as user) to
> display a left column.
>
> I read this:
>
> The view has access to the instance-variables of the controller.
> It''s a lot like they are friend classes in that the instance-data
> simply is available in the view templates, as if it were in the
> same scope.
>
> But that doesn''t seem to be true. So my questions is, how do I get
> the view to see my instance variables?
>
> Thanks!
>
> -Theo
>
>
>
> On 10/14/05, Matthew Denner
<matt.denner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: On
> 10/14/05, Theodore Mills
<twmills-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > I keep getting errors indicating the method cannot be found, but
> as far I
> > as I can tell, what I am doing is no different than the model and
> scaffold
> > methods.
>
> What you have defined in your ApplicationController is an instance
> method, and what you are calling in your UserController is a class
> method, so the errors are correct.
>
> But your problems run slightly deeper than this because, if you simply
> change your method definition to:
>
> class ApplicationController < ActionController::Base
> def self.show_left_column (bool)
> @show_left_column = bool
> end
> end
>
> Note the ''self'' bit on the method definition, which
defines it as a
> class method.
>
> Then this will likely complain too because, in Ruby, @show_left_column
> is a declaration of an instance variable, meaning that each instance
> of ApplicationController will have it''s own copy (and therefore
could
> be different from any other).
>
> If you change the code to:
>
> class ApplicationController < ActionController::Base
> def self.show_left_column (bool)
> @@show_left_column = bool
> end
> end
>
> Then @@show_left_column is a class variable meaning that all instances
> of the ApplicationController will share this value, so it is not
> different for each instance.
>
> I probably haven''t explained that at all well but, if you
don''t have
> it, I highly recommend the Programming Ruby book
> (http://www.pragmaticprogrammer.com/titles/ruby/index.html) as it
> *will* teach you Ruby properly. There''s a copy of the 1st edition
> online (http://www.rubycentral.com/book/) but it''s for pre-1.8
> versions of Ruby so is a little out-of-date. If you look at the
> "Class Variables and Class Methods" section on
> http://www.rubycentral.com/book/tut_classes.html you''ll get an
idea of
> what I''m trying to say about your problem.
>
> Matt
> _______________________________________________
> 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
>
-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org