在 Ruby on Rails 中添加新页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5911794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:05:30  来源:igfitidea点击:

Adding a new page in Ruby on Rails

ruby-on-railsrubyruby-on-rails-3

提问by McStretch

Quite new to Ruby on Rails and I am stuck on probably an easy task. Basically I am working on a colleague's app and need to add an additional page that shows users how the app works. I have already written the HTML and styles. I just don't know how to exactly add it to Rails and configure the routes properly. Any help would be appreciated!

Ruby on Rails 的新手,我可能被困在一个简单的任务上。基本上我正在开发一个同事的应用程序,需要添加一个额外的页面来向用户展示该应用程序的工作原理。我已经编写了 HTML 和样式。我只是不知道如何将它准确添加到 Rails 并正确配置路由。任何帮助,将不胜感激!

回答by McStretch

Firstmake sure that your colleague did not already create a controller to handle static pages. Look under app/controllersfor controllers titled anything similar to directories_controlleror pages_controller, etc. If he/she has, follow the pattern your colleague already set up (you might want to ask him/her for guidance at that point). If there is not static pages controller like that, then follow the advice below.

首先确保您的同事尚未创建控制器来处理静态页面。在下方查找app/controllers名称类似于directories_controllerpages_controller等的控制器。如果他/她有,请遵循您的同事已经设置的模式(此时您可能需要向他/她寻求指导)。如果没有这样的静态页面控制器,那么请遵循以下建议。

You can create a controller named something like PagesControllerthat defines methods that match a route. For instance, your additional page might be called "help", in which case you can define a controller like so:

您可以创建一个名称类似于PagesController定义匹配路由的方法的控制器。例如,您的附加页面可能被称为“帮助”,在这种情况下,您可以像这样定义控制器:

class PagesController < ActionController::Base
  def help
    # put any code here that you need 
    # (although for a static view you probably won't have any)
  end
end

Then you'll want to create a new folder under app/viewstitled pages, and you can add your static page there (app/views/pages) with a .erbextension. Using a .erbwill allow your new page to use the default layout.

然后,您需要在app/viewstitled下创建一个新文件夹pages,您可以在那里添加带有.erb扩展名的静态页面(app/views/pages)。使用 a.erb将允许您的新页面使用默认布局。

Finally, you'll need to add this controller to routes.rbin (config/routes.rb)to tell rails where to look for the /helppage:

最后,您需要将此控制器添加到routes.rbin(config/routes.rb)以告诉 rails 在哪里查找/help页面:

match '/help' => 'pages#help'

回答by edgerunner

Static pages

静态页面

If your pages are truly static(nothing dynamic in them), you can drop them in the /publicdirectory, and they will be directly accessible.

如果您的页面确实是静态的(其中没有任何动态),您可以将它们放在/public目录中,它们将可以直接访问。

A file in ../public/help.htmlwill be accessible at http://yourdomain.com/help.html

../public/help.html可以在以下位置访问文件http://yourdomain.com/help.html

Semi static pages

半静态页面

If you want to use your layouts but just provide static content, you can make a controller, routes and views for that as follows.

如果你想使用你的布局但只提供静态内容,你可以为它创建一个控制器、路由和视图,如下所示。

# static_controller.rb
class StaticController < ApplicationController
  def show
    render params[:page]
  end
end

# towards the end of routes.rb
get "/:page" => "static#show"

And make your views in app/views/static. You can use just plain html or erb as well. A view called help.html.erbwill be available at http://yourdomain.com/help

并在app/views/static. 您也可以仅使用纯 html 或 erb。一个名为的视图help.html.erb将在http://yourdomain.com/help

Any view file you create under app/views/staticwill be available without modifying your routes or controller.

您在其下创建的任何视图文件app/views/static都可以使用,而无需修改您的路由或控制器。