Newbie: Static page without controller

Hello,

I’ve been through a few tutorials and have made some simple apps that
interact with the database, but I’m having trouble doing something that
seems like it would be very simple.

Just imagine a static website that doesn’t interact with the database.
You have pages like:
www.domain.com/home
www.domain.com/services
www.domain.com/more-stuff

Since these are static pages, I’d like to just create an rhtml file for
them and have them accessible from the root, as shown above.

So my question is, how do I get a static ®html page to show on the
root of the url? Creating a controller for each one seems to make it
only accessible via a subfolder of the controller
(www.domain/controller/services). Do I really have to create a
controller for each of these pages? Is that the best way? What folder
(under /views I assume) should these rhtml files go in?

This seems like such a simple thing, but I just can’t seem to figure it
out.

Thank you,

Brandon

There are two ways to do this:

  1. Create controllers for every page, and put the static content in a
    “index.rhtml” view.

  2. Use routes.rb to route these to a “static” controller:
    2.1 Create a new controller called StaticController
    2.2 Add the routes to routes.rb:
    map.connect ‘/services’, :controller => ‘static’, :action => ‘services’
    2.3 Put a services.rhtml in the app/views/static/ directory

I hope this helps,

Jules

Jules J. wrote:

There are two ways to do this:

  1. Create controllers for every page, and put the static content in a
    “index.rhtml” view.

  2. Use routes.rb to route these to a “static” controller:
    2.1 Create a new controller called StaticController
    2.2 Add the routes to routes.rb:
    map.connect ‘/services’, :controller => ‘static’, :action => ‘services’
    2.3 Put a services.rhtml in the app/views/static/ directory

Or just put html files in public/…

Hi, Brandon.

You can drop your static files in your RAILS_ROOT/public/ directory.
Remember, RAILS_ROOT in this case is not where Rails is installed,
but rather the top level directory of your application. The public/
directory lives at the same level as app/, components/, config/, etc.
Then you can access your static pages as www.domain.com/home.rhtml, etc.

You could also create a single controller to handle static content
and use the standard Rails file layout; e.g.,

app/controllers/static_controller.rb with methods like index(), home
(), services(), etc.
app/views/static/index.rhtml
app/views/static/home.rhtml, etc.

You would then access your pages by visiting www.domain.com/static/
index, www.domain.com/static/home, etc.

I hope this makes sense. Feel free to ping me off-list if you’d like
to talk about this further.

Regards,
David

Thank you for your quick response.

Which way do most people use? (I assume this is a common thing)

Creating controllers for each would be nice if you’re going to end up
expanding the site. But if you have 100 static pages, that’s a lot of
empty controllers, and a lot of folders with one file, which seems
unnecessary. Plus, it seems like it would make more sense to name your
.rhtml file after the page name.

Option number 2 seems better to me. Would I still have to add a line for
each static page? I suppose that’s not such a big deal, although your
routes.rb file could potentially get large. And if you deleted or
renamed a page, you’d need to remember to make the change there, too.

Is there a way to just have a default controller that takes the page
(url) you’ve requested, and checks to see if that file exists in the
views/static/ directory, and if so, displays it? (And if not, give a
404?)

I guess somehow you’d have to also make sure that your static page names
don’t conflict with other controller names you have. Or you could just
say that if another controller exists with that name, it should take
precedence and run that controller. Otherwise, load the static page.

Does this sound like a good solution? Is it possible? (I created
something similar with PHP/Smarty once…)

Thank you.

Brandon

On Jan 25, 2006, at 11:19 AM, Jules J. wrote:

2.3 Put a services.rhtml in the app/views/static/ directory

I hope this helps,

Jules

  1. If your pages are truly just static html pages you can just put
    them in the public directoy and they will ‘just work’.
    So if you had a file called home.html in your public dir you could
    access it with this url: www.domain.com/home
    Rails will automatically look for a file with the html exgtension in
    public before it send the request through the dispatcher.

Cheers-
-Ezra Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
[email protected]

Brandon,

Assuming that your static pages do not need the layout templates from
your controllers you can simply put the folders in your public
directory. However, you’ll need to specify the full url to the html
file:

www.domain.com/home/index.html

Alternatively, you can create a StaticController that simply has
empty methods for each page:

class StaticController < ActionController
layout ‘MyOtherController’

def home
end

def services
end

end

Wow, I had more responses while I was replying. Thanks, guys - this
forum is great!

Putting the files in the /public/ folder sounds like the solution I was
looking for! So simple. Thanks!

But if I’m understanding correctly - if I put the rhtml files in the
/public/ folder, those pages won’t be able to use the same layout that
the application controller defines? (meaning each static page would have
to start with an html element and have the header and navigation, etc.?)

So if I wanted to use the layout, I would have to create a static
controller of sorts and have separate methods for each page? Is this
the recommended solution? (how do you all do it, or does it depend on
the app?)

Could you create a single function in your static controller that would
somehow handle all page requests without having to edit the file each
time you add a new page?

David R. wrote:

Hi, Brandon.

You can drop your static files in your RAILS_ROOT/public/ directory.
Remember, RAILS_ROOT in this case is not where Rails is installed,
but rather the top level directory of your application. The public/
directory lives at the same level as app/, components/, config/, etc.
Then you can access your static pages as www.domain.com/home.rhtml, etc.

You could also create a single controller to handle static content
and use the standard Rails file layout; e.g.,

app/controllers/static_controller.rb with methods like index(), home
(), services(), etc.
app/views/static/index.rhtml
app/views/static/home.rhtml, etc.

You would then access your pages by visiting www.domain.com/static/
index, www.domain.com/static/home, etc.

I hope this makes sense. Feel free to ping me off-list if you’d like
to talk about this further.

Regards,
David

On Jan 25, 2006, at 11:38 AM, Brandon P. wrote:

Option number 2 seems better to me. Would I still have to add a
I guess somehow you’d have to also make sure that your static page
Brandon
Brandon-

Here is a way to have a static controller that will automatically

look for a .rhtml view file that matches the url requested without
the need to add an action for each page:

class StaticController < ApplicationController

def method_missing(action_name)
	# you can add a layout to this render call if you want to use one.
	render :action => action_name.to_s
end

end

With that controller a request to http://example.com/static/foo

would render the app/views/static/foo.rhtml view template. This
allows you to add as many static templates to the app/views/static
dir as you want and they will all be picked up automatically when you
add them. If they don’t exist a request will just invoke your 404
error handler.

Cheers-
-Ezra Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
[email protected]

Thanks!

That last solution looks great!
Now to my original question:
How do you make it work going to:

www.domain.com/foo

instead of:

www.domain/static/foo

Routes to the rescue.

In your routes.rb file, before the map.catchall line, you can add
something
like this:

map.connect ":action", :controller => 'static'

Now /foo will invoke StaticController#foo

You don’t even need that much code. Rails will render the view if there
is
no action in the controller and the view template exists, e.g. This
will
work:

URL -
/static/foo

Controller - static_controller.rb -
class StaticController < …
# No Actions Defined
end

View - static/foo.rhtml -

Whatever you want to display

Add as many view files as you want without changing the controller.

You can add to the controller to set your layout for static files:

Controller -
class StaticController < …
layout :static_layout
# No Actions Defined
end

Yes, but the line for the static controller must be after the ones for
the
specific controllers. Routes are processed in order, and the first one
to
apply to the current request is used.

On 1/25/06, Brandon P. [email protected] wrote:

domain.com/whatever - goes to a static page
domain.com/customers/add - goes to the add customer page

Still possible with this configuration?

Yes, keep in mind however:

domain.com/customers - goes to static/customers, not customers/index

For those who have developed apps that have different levels of account
quotas in regards to total file upload space, how are you managing it?
Are you keeping track of the total uploaded bytes in an Account model or
are you using OS quota mechanisms?

Any suggestions would be appreciated.

Thanks,

Sean

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

Now /foo will invoke StaticController#foo

Cool. And will I still be able to have non-static pages run by other
controllers the normal way? Or will this make everything go through the
static controller?

Example:
domain.com/services - goes to a static page
domain.com/whatever - goes to a static page
domain.com/customers/add - goes to the add customer page

Still possible with this configuration?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs