Rails3 - create a static web page?

I want to create static web pages in my applications - T & Cs, About, Privacy, etc. I could just create blank pages and put them in a shared folder and add href links. Is this considered best practice? or should i use rails g controller for each of them? What's the difference...

+6
source share
6 answers

Often I create a site controller that has actions for each of the public pages, assuming that there will not be tons of content on the public side. If there were more, I would look at some kind of CMS. In any case, create a site controller, and then create routes and templates for each of the pages you need. This way you can use the layout and use the Rails helpers if you need them.

+3
source

HighVoltage is a gem that helps with what you do:

https://github.com/thoughtbot/high_voltage

This makes it easy to deal with these scenarios. From the docs:

Write down your static pages and put them in the RAILS_ROOT / app / views / pages directory.

 $ mkdir app/views/pages $ touch app/views/pages/about.html.erb 

After posting something interesting there, you can associate it with any place in your application:

 link_to "About", page_path("about") 

This will also work if you like a more explicit style:

 link_to "About", page_path(:id => "about") 
+8
source

Of course you can just create about.html etc. and put them in the public folder. If this is just a fully static web page, then the controller does not add any value. Subdirectories also work fine in the public folder, as you would expect.

+1
source

I solved this using this amazing GEM https://github.com/thoughtbot/high_voltage

+1
source

I just realized that this should be pretty easy, hopefully think about it through:

Create a route, for example, for example:

 match '/about' => "static#about" 

~ then create a simple controller, in this case app / controller / static_controller.rb

 class StaticController < ApplicationController respond_to :html def about # nuttin end end 

~ now all we need is a view: (/app/views/static/about.html.erb)

 Hey! 

Sorting

+1
source

Create static page 'home'

 $ rails generate controller Pages home 

Added new "control" pages of the controller with the action "home". The new route is inserted into 'config / routes.rb'

 # config/routes.rb get "pages/home" 

To create a link to the Home page

 <%= link_to "Home", :controller => "pages", :action => "home" %> 
+1
source

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


All Articles