Where to put views for application controller?


#1

hi,

im pretty much a rails newbie, but i was wondering where the rhtml
files go for the application controller?

i.e. if i had some logic that needed to be performed on the document
root of my website: (i.e. http://www.mysite.com)
where would i put this rhtml file.

I tried putting it in the public directory, but that failed…and the
content inside the <% %> takes weren’t evaluated…

also, just entering http://www.mysite.com didn’t automatically render
index.rhtml, but rather gave me a 404 error.


#2

removed_email_address@domain.invalid wrote:

hi,

im pretty much a rails newbie, but i was wondering where the rhtml
files go for the application controller?

The application controller is basicly a controller for all controllers
(that’s what the top line says on most generated controllers:

class ExampleGeneratedController < ApplicationController

this is used for hierarcial ruby reasons to overwrite actions/methods in
“children” Controllers (instead of writing the same action for every
controller, it can be put in the application controller, and Each
subClass controller will have that method - it is very useful for
authentication systems… )

BUT … onto your issue - if you wish to see the homepage root rhtml, it
can be brought up in ANY controller - it’s all a matter of where you
route the root of the site to:

the myapp/config/routes.rb file handles this - all you have to do is
route ‘/’ to a controller and action, and the rhtml corresponding, will
be the rhtml of the root site. so, for example (supposing you have these
controllers and actions):

routes.rb

map.connect ‘/’, :controller => ‘posts’, :action => ‘list’

and then set up the rhtml in /app/view/posts/list to your liking. you
could do this with any controller and any action - there is no
limitation on this … you just have to set it up in the routes.rb
file…

:slight_smile:

hope this helps out…

-shai


#3

Hi

In your config directory is a routes.rb file which rails uses to
understand
which actions in your app to call in response to the url requested.

Here is what it looks like:

ActionController::Routing::Routes.draw do |map|

The priority is based upon order of creation: first created ->

highest
priority.

Sample of regular route:

map.connect ‘products/:id’, :controller => ‘catalog’, :action =>

‘view’

Keep in mind you can assign values other than :controller and

:action

Sample of named route:

map.purchase ‘products/:id/purchase’, :controller => ‘catalog’,

:action
=> ‘purchase’

This route can be invoked with purchase_url(:id => product.id)

You can have the root of your site routed by hooking up ‘’

– just remember to delete public/index.html.

map.connect ‘’, :controller => “welcome”

Allow downloading Web Service WSDL as a file with an extension

instead of a file named 'wsdl

map.connect ‘:controller/service.wsdl’, :action => ‘wsdl’

Install the default route as the lowest priority.

map.connect ‘:controller/:action/:id.:format’
map.connect ‘:controller/:action/:id’
end

As you can see the first uncommented line is
map.connect ‘’, :controller => “welcome”

What this tells rails is that when the url is only http://www.mysite.com
it
should route that request to the welcome controller. If no action is
specified rails will default to calling the index action in that
controller.
The index action will look for an index.rhtml, parse the <% %> into the
appropriate html and deliver a html file to be served to the browser.

The last uncommented line which looks like this
map.connect ‘:controller/:action/:id’

is the default decoding of urls. the routing expects a url that looks
like
this for example:
http://www.mysite.com/welcome/show/5
This route mapping “decodes” the url as
:controller => ‘welcome’
:action => ‘show’
:id => 5

Hope this helps.

Ivor


#4

thanks to both of you…thats clears things up a lot, i will start
playing with it first chance i get.

To delve a bit deeper about the structure of a rails app, is it bad
design to have only an application.rb controller? and so other
controllers.

Let me explain a bit more, basically, the site i am working on has a
lot of cgi script (thats the main chunk of the features this site
promotes), and I have the task of adding user accounts to this site
for a small additional feature. basically, at this point in time, it
seems like this will be the only feature rails will be in charge
of…so i am left wondering whats the point of putting all of my
logic in an AccountController, and having an empty
Application.rb…maybe it would make the most sense to just have
everything inside the application controller, and shorten my urls at
the same time (i.e. http://www.mysite.com/login vs.
http://www.mysite.com/account/login).

This first hit me when i was adding the rails code to existing html
pages, i found that i needed to add a ‘…/’ before the images
directory in all of my tags because of the fact that i was up a
level (i.e. in the account controller)