View displays with no action

I discovered this quite by accident. I have a single controller setup
and it is named, 'main'. If I add a view, /app/views/main/
test1.rhtml, that view is displayed via the URL host/main/test1 even
though there is no action called 'test1' in the main controller. I
always thought the displaying of a view was contingent on the presence
of an action. I know that a view only shares the same name as the
action by default and that that can be changed from within the action;
but, I certainly didn't realize that a view could be accessed in the
total absence of an associated action. Is this truly the way things
are supposed to work? Thanks for any input.

       ... doug

I think of the controller action as a before_render callback which gives me a chance to do any prep work I might need.

I have one view that has no associated controller action, but gets rendered from several other methods that set up some variables in different ways. It's essentially a one-field form, and instead of having a separate .rhtml file for each one, they each set variable that control the labeling and where the submit goes to. It's very convenient.

I have one view that has no associated controller action, but gets
rendered from several other methods that set up some variables in
different ways.

Let me re-phrase that to make sure that I understand:

You have one view that has no default association with any controller
action (i.e., the view does not have the same name as any controller
action). However, the one view is associated on a non-default basis
(presumably by means of render) with several different actions. Then
you use each action to configure what your one view is going to do.
Cool. You also could have had your view be the default view for one
of your actions, right?

I think of the controller action as a before_render callback which
gives me a chance to do any prep work I might need.

I have difficulty with thinking of it that way because it's the action
that calls the view not the other way around. That's why it sort of
upsets my universe to learn that apparently a view can be called
directly without having an action associated with it. It appears that
it is like every view has a default empty action in the appropriate
controller if there is no explicit action in the controller. That
takes some getting used
to.

Thanks for the input.

      ... doug

[snip]

Rails first determins the controller and action from your routes.rb
patterns. Then it looks for the controller, which must exist. It
will also require a view template that corresponds to the action
name. Code in your controller for the action is totally optional. If
you provide a method in your controller with the same name as the
action, Rails will call it before it renders your view, giving you a
chance to run whatever code you want first. Unless you explicity
redirect or render a different action in that code, it will continue
to search for and render the view.

Does that help? Basically, controller code for a given action is
optional, not required - only the view is required.

Jeff
softiesonrails.com
essentialrails.com

Does that help?

It helps a whole heck of a lot. One thing I think I learned that I
was apparently misinformed about is the definition of 'action'. I
thought that the method in the controller *WAS* the action.
Apparently, that's not the case. Apparently the action is determined
from patterns contained in the routes.rb file and then we may
optionally include a method with the same name as the action in the
controller.

I think when you said:

It will also require a view template that corresponds
to the action name.

it was intended to mean that unless code in the controller method of
the same name as the action specifies otherwise.

Finally, I would just like to point one thing out that I think may
have contributed to my misunderstanding. That thing is that if you
call main/doc1 and no template for doc1 exists, the error message is
that 'No action responded to doc1.' I think that really the error
that it is talking about is that the template is missing, not the
action.

Thanks ever so much for the input. It has been most helpful

     ... doug

The application is an in-house one, and when something unusual happens we need a special way to handle those events for a given client.
It's outside of the normal work-flow, and they don't come up often but I had to make a way to quickly deal with it.

So I have a file called set_client.rhtml, which contains a one-field form.
I have no set_client method in my controller. Other methods will do something like this:

     @headline = "Send money to this client"
     @action_to_call = "special_payment"
     render :action => :set_client

I'm not allowed to use any Javascript, and before they can actually handle the problem the person usually has to review a list of items related in one way or another to this client. So this way I can take them to the proper place and at the same time show the the correct list of items.

The form looks something like this:

<p>
  <%= @headline.titlecase -%>
</p>
   <%= form_tag :action => @action_to_call %>
    <p>
    <%= text_field_tag :client_id, params[:client_id] %>
    </p>
    <p>
    <%= submit_tag "Next..." %>
    </p>
  <%= end_form_tag %>

And so the submitted form will go where ever @action_to_call says.
I'm sure there's a more clever way to handle this, but this does what I need for those out-of-the-normal-flow cases.

You don't need the template if your controller doesn't let it get that far. So either the controller, the template, or both have to be there.
If your controller method always redirects or renders then rails will never even look for the template.