# View displays with no action

**URL:** <https://www.ruby-forum.com/t/view-displays-with-no-action/121233>\
**Category:** Rails\
**Created:** [November 25, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/view-displays-with-no-action/121233 "2007-11-25T21:18:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [November 25, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/view-displays-with-no-action/121233/1 "2007-11-25T21:18:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [November 25, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/view-displays-with-no-action/121233/2 "2007-11-25T21:28:26Z")

</div>

On Nov 25, 2007, at 2:18 PM, doug wrote:

> 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. …  
> Is this truly the way things  
> are supposed to work?

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.

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [November 25, 2007, 11:18pm UTC](https://www.ruby-forum.com/t/view-displays-with-no-action/121233/3 "2007-11-25T23:18:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [November 25, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/view-displays-with-no-action/121233/4 "2007-11-25T23:31:22Z")

</div>

On Nov 25, 2:18 pm, doug [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

[essentialrails.com](http://essentialrails.com)

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [November 26, 2007, 3:28am UTC](https://www.ruby-forum.com/t/view-displays-with-no-action/121233/5 "2007-11-26T03:28:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [November 26, 2007, 3:44am UTC](https://www.ruby-forum.com/t/view-displays-with-no-action/121233/6 "2007-11-26T03:44:26Z")

</div>

On Nov 25, 2007, at 4:18 PM, doug wrote:

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

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:

\<%= @headline.titlecase -%\>

 \<%= form\_tag :action =\> @action\_to\_call %\> 

\<%= text\_field\_tag :client\_id, params[:client\_id] %\>

\<%= submit\_tag "Next..." %\>

 \<%= 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.

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [November 26, 2007, 3:47am UTC](https://www.ruby-forum.com/t/view-displays-with-no-action/121233/7 "2007-11-26T03:47:56Z")

</div>

On Nov 25, 2007, at 8:27 PM, doug wrote:

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

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.
