views:

97

answers:

5

I'm a beginner of Ruby and Rails, so this is probably an easy question.

How should I set up a simple page that does not need to have any own database tables? In my case, for example, I have a site storing songs and artists. How I want just a simple HELP page with no intelligence, just static html. I also need a BROWSE page, where the user will select whether to browse for artists or songs. This page will not have any database tables, however it will have a list of links from A-Z, provided the number of posts for each letter, so therefore it needs to have database interaction for tables it does not own by itself.

Should I just create controllers for HELP and BROWSE, or will they need models as well? Using Rails 2, which script/generate tools should I use and what should I ask them to do for me?

+1  A: 

Controllers and models are not hardly connected in Rails. It is just a convention. So you can easily create a controller, which will not be connected with any model.

Dmitry Maksimov
+1  A: 
John Topley
+1 Thank you! This is very similar to the one that I accepted. Had to pick one. Sorry. :)
Johan
The pages_path('help') creates a link to /pages.help instead of /pages/help. How can I change that?
Johan
+1  A: 

I usually create a PagesController that shows the static pages like about, faq or privacy.

What you have to do is generate the controller by using

script/generate controller pages

then add the following in your config/routes.rb

map.resources :pages, :only => :show

In your PagesController

def show
  # filter the params[:id] here to allow only certain values like
  if params[:id].match /browse|help/
    render :partial => params[:id]
  else
    render :file => "/path/to/some/404_template", :status => 404
  end
end

Then you just need to add partials in app/views/pages/

#in /app/views/pages/_help.html.erb

<p>This is the help section</p>
jvnill
Thanks! There were several good candidates for acceptance, but now I've tried them all and this is the one I'll go for. (However, I guess you mean script/generate controller pages. That works anyway).
Johan
Using this method, how can I create a link to /pages/help ? <%= link_to 'Help', pages_path('help') %> creates a link to /pages.help .
Johan
Thanks for the correction Johan. I updated the post. As for your question, use page_path('help') instead of pages_path.
jvnill
+1  A: 

Well if it's pure static (as in *.html) you can just add it in your public folder. For example:

public/test.html public/hello.html public/about.html

Ed
+1 Thanks! It's good to know that this is possible. However, in this case, I like it to make use of a common layout.
Johan
+1  A: 

it is not compulsory to have a model for each controller. it is just a convention to easily relate

let us consider the page that you want to display is about_us

add a controller about_us.rb

add a view about_us/index.html.rb

if you want the view not to follow any layout just say

 render :layout => false

in your about_us.rb

ZX12R
+1 Thanks! Good to know that this is possible. I went for another way of the suggestions though.
Johan