What goes where?/Scope question

In a couple of parts …


In a controller:

The controller can use anything defined in
app/controllers/application_controller.rb

What else can it use?

  • app/helpers/application_helper.rb because if the above?
  • app/helpers/_helper.rb ?
  • any other helpers

What code in models can it use?


In view, what is the scope of the variables?
Do they have to be defined in the relevant controller?

What about the main template?
What about “other” partials?

What methods can it use?
Just the ones for the the controller?
What about methods for the models?
What about things defined in the libraries?


I wonder why. I wonder why.
I wonder why I wonder.
I wonder why I wonder why
I wonder why I wonder!
– Richard Feynman

On 11 Dec 2007, at 21:07, Anton J Aylward wrote:

In a couple of parts …


In a controller:

The controller can use anything defined in
app/controllers/application_controller.rb

All your controllers inherit from ApplicationController, so any
instance methods there are available from any instance of your
controllers

What else can it use?

  • app/helpers/application_helper.rb because if the above?
  • app/helpers/_helper.rb ?
  • any other helpers

Helper methods are (by default) only available in the appropriate view

What code in models can it use?
I think this question belies a misunderstanding of what is going on.
You can from a controller call any of the class methods for a model
(eg find), and if you have an instance of a model then you can call
any of its public methods. This isn’t a rails question though - it’s
just the way the language is.


In view, what is the scope of the variables?
Do they have to be defined in the relevant controller?
Instance variables are copied accross. That’s all

What about the main template?
What about “other” partials?

What methods can it use?
Helper methods (by default application_helper and the helper
associated with the controller, but you can specify extra modules/
functions)

Fred

Frederick C. said the following on 11/12/07 04:23 PM:

All your controllers inherit from ApplicationController, so any
instance methods there are available from any instance of your
controllers

So in one controller I can’t access code from another controller?
I can’t even write

a = OtherController.new

to get to them.

What else can it use?

  • app/helpers/application_helper.rb because if the above?
  • app/helpers/_helper.rb ?
  • any other helpers

Helper methods are (by default) only available in the appropriate view

So I can’t access the helper methods for a controller?
I seem to have helpers for each controller.
Can I only access code in them from the view corresponding to that
controller and not the controller?

What code in models can it use?

I think this question belies a misunderstanding of what is going on.

:slight_smile: Trying to be comprehensive :slight_smile:

You can from a controller call any of the class methods for a model
(eg find), and if you have an instance of a model then you can call
any of its public methods. This isn’t a rails question though - it’s
just the way the language is.


In view, what is the scope of the variables?
Do they have to be defined in the relevant controller?

The examples I look at, for example instiki, seem to have an awful lot
defined in the controller.

Instance variables are copied accross. That’s all

What about the main template?
What about “other” partials?

What methods can it use?

Helper methods (by default application_helper and the helper
associated with the controller, but you can specify extra modules/
functions)

I presume you mean libraries.
I understand ‘include’, but does that mean stuff in the views get access
to all code that’s on the PATH?

I ask this because a view, perhaps the template, may be accessing things
that don’t seem to appear in the controller. Am I seeing a side effect
of the search PATH?

I realise that there shouldn’t be code in the view, but decisions about
what to display seem OK, like this from instiki

<% if @page and (@page.name == 'HomePage') and (%w( show published print ).include?(@action_name)) %> <%= h @web.name %> <% elsif @web %> <%= @title %> in <%= h @web.name %> <% else %> <%= @title %> <% end %> <%= @show_diff ? ' (changes)' : '' %>

Fred

Just the ones for the the controller?
What about methods for the models?
What about things defined in the libraries?


Few problems cannot be solved by proper application of high explosives.

Frederick C. said the following on 12/12/07 09:41 AM:

reference an undefined constant it will try and load the appropriate
file (so referencing Customer loads customer.rb automatically).

So putting something like this (and maybe more lines in it) in
config/environment.rb is of no use, then?

config.load_paths += %w(
app/controllers
app/models
app/helpers
lib
app/controllers/admin
app/helpers/admin
).map { |path| File.join(RAILS_ROOT, path) }


Things are more like they are now than they ever were before.
Dwight D. Eisenhower

On Dec 12, 8:37 pm, Anton J Aylward [email protected] wrote:

            app/helpers/admin
).map { |path| File.join(RAILS_ROOT, path) }

Those are already all on the load path. Like I said the load path only
influences whether when you say Customer whether rails will be able to
track down customer.rb. That’s all it does.

Fred

On 11 Dec 2007, at 22:11, Anton J Aylward wrote:

So in one controller I can’t access code from another controller?
I can’t even write

a = OtherController.new

to get to them.
Well assuming that the constructor for a controller takes no
arguments, you could then do a.something. It’s pretty much guaranteed
to not be what you really want.

associated with the controller, but you can specify extra modules/
effect
of the search PATH?

I think you have a deep misunderstanding of what’s going on. There is
no search path for ‘code’. Rails performs some magic so that if you
reference an undefined constant it will try and load the appropriate
file (so referencing Customer loads customer.rb automatically).

Fred