What goes where?/Scope question

In a couple of parts ...

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/<controller-base-name>_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 Cheung said the following on 11/12/07 04:23 PM:

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

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/<controller-base-name>_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

  <title>
    <% 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)' : '' %>
  </title>

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.

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 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

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

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 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).

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) }

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) }

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