开发者

Do even simple pages need routes in Rails?

开发者 https://www.devze.com 2023-03-07 23:45 出处:网络
I wanted to make a simple page for a footer of a site discussing the team of the project.开发者_StackOverflow中文版 Such a page usually just has information and nothing too fancy.

I wanted to make a simple page for a footer of a site discussing the team of the project.开发者_StackOverflow中文版 Such a page usually just has information and nothing too fancy.

I didn't make a route for it, and just basically saved it as team.html.rb in the folder: /app/views/team.html.rb

I am not sure whether that is the right place for such a file to be saved to. Where is the best place for such a one-off file?

Also, do I still need to make a route and a controller for this, or can I just skip those for such a simple page?

Thanks!!


I'm using a controller to deliver static pages.

Here's a link to a similar question:

How to do static content in Rails?

What you can do also is put a static html file into the 'public' folder and refer to it like this:

http://localhost:3000/name-of-static-file


I don't think that will work because Rails expects all requests to go through a controller. The simplest thing would be to do something like this at the end of your routes.rb:

match "/:action" => "pages"

Then in app/views/pages you can put your team.html.erb. You also will probably need at least a blank controller in app/views/pages_controller.rb:

class PagesController < ApplicationController
end

The nice thing about this is if you happen to need some dynamic content for one of the "static" pages you can just define a controller method to load it.

If this seems like overkill for your site, you might want a lighter framework like Sinatra


routes point to controller actions, not views.

a request to /team would (iirc) point to application#team. If that action doesn't exist, you will get an error. If you DO have that action defined (with a call to render, it will look in /app/views/application for a file called team.html[.erb|.json|.etc]

As the related thread points out, thoughtbot's high_voltage gem is great for a base of serving static pages.

0

精彩评论

暂无评论...
验证码 换一张
取 消