在Ruby on Rails中添加新页面

时间:2022-07-05 07:32:11

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并正确配置路由。任何帮助,将不胜感激!

2 个解决方案

#1


38  

First make sure that your colleague did not already create a controller to handle static pages. Look under app/controllers for controllers titled anything similar to directories_controller or 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.

首先确保您的同事尚未创建控制器来处理静态页面。在应用程序/控制器下查找名称类似于directories_controller或pages_controller等的控制器。如果他/她有,请按照您的同事已经设置的模式(您可能希望在此时向他/她寻求指导)。如果没有这样的静态页面控制器,请遵循以下建议。

You can create a controller named something like PagesController that 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/views titled pages, and you can add your static page there (app/views/pages) with a .erb extension. Using a .erb will allow your new page to use the default layout.

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

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

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

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

#2


18  

Static pages

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

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

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

可以在http://yourdomain.com/help.html访问../public/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.erb will be available at http://yourdomain.com/help

并在app / views / static中创建您的视图。你也可以使用普通的html或erb。 http://hourdomain.com/help上将提供名为help.html.erb的视图

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

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

#1


38  

First make sure that your colleague did not already create a controller to handle static pages. Look under app/controllers for controllers titled anything similar to directories_controller or 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.

首先确保您的同事尚未创建控制器来处理静态页面。在应用程序/控制器下查找名称类似于directories_controller或pages_controller等的控制器。如果他/她有,请按照您的同事已经设置的模式(您可能希望在此时向他/她寻求指导)。如果没有这样的静态页面控制器,请遵循以下建议。

You can create a controller named something like PagesController that 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/views titled pages, and you can add your static page there (app/views/pages) with a .erb extension. Using a .erb will allow your new page to use the default layout.

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

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

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

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

#2


18  

Static pages

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

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

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

可以在http://yourdomain.com/help.html访问../public/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.erb will be available at http://yourdomain.com/help

并在app / views / static中创建您的视图。你也可以使用普通的html或erb。 http://hourdomain.com/help上将提供名为help.html.erb的视图

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

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