简体   繁体   English

Ruby on rails简单页面没有数据库模型

[英]Ruby on rails simple page without database model

I'm a beginner of Ruby and Rails, so this is probably an easy question. 我是Ruby和Rails的初学者,所以这可能是一个简单的问题。

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. 我只想要一个没有智能的简单帮助页面,只需要静态HTML。 I also need a BROWSE page, where the user will select whether to browse for artists or songs. 我还需要一个BROWSE页面,用户将选择是否浏览艺术家或歌曲。 This page will not have any database tables, however it will have a list of links from AZ, provided the number of posts for each letter, so therefore it needs to have database interaction for tables it does not own by itself. 此页面将没有任何数据库表,但是它将具有来自AZ的链接列表,提供每个字母的帖子数,因此它需要为其本身不拥有的表进行数据库交互。

Should I just create controllers for HELP and BROWSE, or will they need models as well? 我应该只为HELP和BROWSE创建控制器,还是需要模型? Using Rails 2, which script/generate tools should I use and what should I ask them to do for me? 使用Rails 2,我应该使用哪些脚本/生成工具,我应该让他们为我做什么?

I usually create a PagesController that shows the static pages like about, faq or privacy. 我通常创建一个PagesController来显示静态页面,如about,faq或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 然后在config/routes.rb添加以下config/routes.rb

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

In your PagesController 在您的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/ 然后你只需要在app/views/pages/添加部分app/views/pages/

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

<p>This is the help section</p>

I've used the approach shown below in the past. 我过去曾使用过如下所示的方法。 Set up a named route in config/routes.rb : config/routes.rb设置命名路由:

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

—Note that the :page parameter is constrained to certain values (the URLs /browse and /help ). - 表示:page参数被约束为某些值(URL /browse/help )。 With this route in place you can create links like this: 使用此路线,您可以创建如下链接:

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

Finally create a controller ( app/controllers/pages_controller.rb ): 最后创建一个控制器( app/controllers/pages_controller.rb ):

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

You can change the show method within the controller if you require more flexibility. 如果需要更大的灵活性,可以在控制器中更改show方法。 For example, you might want to use different layouts for different pages. 例如,您可能希望对不同的页面使用不同的布局。 For that scenario you can branch the logic based on the value of the incoming :page parameter. 对于该场景,您可以根据incoming :page参数的值分支逻辑。

Controllers and models are not hardly connected in Rails. 控制器和模型在Rails中几乎没有连接。 It is just a convention. 这只是一个惯例。 So you can easily create a controller, which will not be connected with any model. 因此,您可以轻松创建一个控制器,该控制器不会与任何型号连接。

Well if it's pure static (as in *.html) you can just add it in your public folder. 好吧,如果它是纯静态的(如在* .html中),你可以将它添加到你的公共文件夹中。 For example: 例如:

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

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 让我们考虑您要显示的页面是about_us

add a controller about_us_controller.rb with it mentioned in routes.rb 添加控制器about_us_controller.rb与它在routes.rb中提到的

add a view about_us/index.html.rb 添加一个关于_us / index.html.rb的视图

if you want the view not to follow any layout just say 如果你想让视图不遵循任何布局,只需说

 render :layout => false

in your about_us.rb 在你的about_us.rb中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM