Ruby on rails simple page without a database model

I am starting Ruby and Rails, so this is probably a simple question.

How do I set up a simple page that does not need any of my own database tables? In my case, for example, I have a website where songs and artists are stored. How I want just a simple HELP page without intelligence, just static html. I also need a BROWSE page where the user will choose whether to search for artists or songs. This page will not have database tables, however it will have a list of links from AZ, subject to the number of messages for each letter, so this requires interaction with the database for tables that it does not have.

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

+3
source share
5 answers

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

What you need to do is generate a controller using

script/generate controller pages

then add the following to config/routes.rb

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

In your web browser

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 partial values ​​to app/views/pages/

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

<p>This is the help section</p>
+5
source

I have used the approach shown below in the past. Configure the named route in config/routes.rb:

map.page ':page', :controller => 'pages', :action => 'show',
         :page => /browse|help/

— , :page (URL /browse /help). :

<%= link_to 'Help', pages_path('help') %>

, (app/controllers/pages_controller.rb):

class PagesController < ApplicationController
  def show
    render params[:page] # => renders /app/views/pages/<page>.html.erb
  end
end

show , . , . :page.

+2

Rails. . , , - .

+1

, ( *.html), . :

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

+1

. ,

consider the page you want to display, about_us

add about_us_controller.rb controller mentioned in routes.rb

add view about_us / index.html.rb

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

 render :layout => false

in your about_us.rb

+1
source

Source: https://habr.com/ru/post/1758344/


All Articles