Forum: Ruby on Rails where to put views for application controller?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7da4a7b5be7f1e115861a253afb80641?d=identicon&s=25 gmoniey@gmail.com (Guest)
on 2007-05-03 07:57
(Received via mailing list)
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.
4c3acdc3d93f54cc7a7281780ec8a4ee?d=identicon&s=25 shai (Guest)
on 2007-05-03 10:01
gmoniey@gmail.com 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...

:)

hope this helps out...

-shai
Af9a9013100ddf2dbb0ca13487b60f8b?d=identicon&s=25 Ivor Paul (Guest)
on 2007-05-03 11:53
(Received via mailing list)
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
7da4a7b5be7f1e115861a253afb80641?d=identicon&s=25 gmoniey@gmail.com (Guest)
on 2007-05-03 19:05
(Received via mailing list)
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 <img> tags because of the fact that i was up a
level (i.e. in the account controller)
This topic is locked and can not be replied to.