Ruby on Rails의 정적 페이지
다음과 같은 페이지가있는 Ruby on Rails 애플리케이션을 만드는 표준 방법은 무엇입니까?
- 집
- 약
- 접촉
나는 그런 행동으로 간단한 웹앱을 만드는 법을 배우고 싶기 때문에 누군가가 보석을 사용한다고 말하는 것보다 링크 나 답변이 있다면 감사 할 것입니다.
해당 페이지의 콘텐츠를 처리하려는 방법에 따라 다릅니다.
접근 방식 # 1-뷰에 콘텐츠 저장
모든 콘텐츠를 ERB보기에 넣으려는 경우 매우 간단한 방법은 PagesController
정적 페이지를 처리하는 데 목적이 있는를 만드는 것입니다. 각 페이지는 컨트롤러에서 하나의 작업으로 표시됩니다.
pages_controller.rb :
class PagesController < ApplicationController
def home
end
def about
end
def contact
end
end
route.rb :
match '/home' => 'pages#home'
match '/about' => 'pages#about'
match '/contact' => 'pages#contact'
그런 다음 app / views / pages 아래에 home.html.erb, about.html.erb 및 contact.html.erb보기를 만듭니다. 이러한보기에는 정적 페이지에서 원하는 모든 콘텐츠가 포함됩니다. 기본적으로 앱의 application.html.erb 레이아웃을 사용합니다.
또한 성능 향상을 위해 페이지 캐싱 을 살펴보고 싶을 것 입니다.
접근법 # 2-데이터베이스에 콘텐츠 저장
내가 사용한 또 다른 접근 방식은 정적 페이지를위한 매우 기본적인 CMS를 만드는 것입니다. 이 경우 페이지가 모델에 표시됩니다. friendly_id gem 을 사용하여 각 페이지의 슬러그를 처리하므로 ID가 아닌 URL (예 : / about)에서 예쁜 이름으로 검색 할 수 있습니다.
page.rb :
class Page < ActiveRecord::Base
attr_accessible :title, :content
validates_presence_of :title, :content
has_friendly_id :title, :use_slug => true, :approximate_ascii => true
end
pages_controller.rb :
class PagesController < ApplicationController
def show
@page = Page.find(params[:id])
render 'shared/404', :status => 404 if @page.nil?
end
end
show.html.erb :
<%= raw @page.content %>
route.rb :
match '/:id' => 'pages#show'
참고 : 모든 항목과 일치하므로이 항목을 routes.rb 끝에 넣으십시오.
Then how you want to create, edit and update pages are up to you - you can have an admin interface, or build it in to your public interface somehow. This approach can benefit from page caching too.
Another option is the high_voltage
gem: https://github.com/thoughtbot/high_voltage
This makes it super easy to create static pages where the content is stored in views.
Jeff's approach #1 (storing content in views and having a route and controller action for each static page) is a good one. The only thing I would add is to use the controller
macro in your routes.
So, instead of this:
match '/home' => 'pages#home'
match '/about' => 'pages#about'
match '/contact' => 'pages#contact'
You can do this:
controller :pages do
get :home
get :about
get :contact
end
It's two extra lines, yet much so much more elegant, since it eliminates repetition and groups your static page routes together visually.
It also uses the get
http verb method instead of match
, which is a better practice for Rails routes (and more concise, now that Rails 4 requires the http verb to be specified when using match
.
Jeff's Approach #1 works great for me. Here is a trick to make the controller dynamically look up pages. With this, you don't need to touch the controller nor the routes.rb for adding pages. Just drop the pages under app/views/pages and the controller will find it.
class PagesController < ApplicationController
def show
render params[:id]
end
end
Check out Michael Hartl's http://railstutorial.org which comes in a 2.3.8 and 3.0.x version. It covers this with great examples and leads you through building them very early on and you will also have the opportunity to learn a lot more than this example. I highly recommend it.
config/routes.rb
get ':id', to: 'pages#show'
app/controllers/pages_controller.rb
class PagesController < ApplicationController
def show
begin
render params[:id]
rescue ActionView::MissingTemplate
render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found
end
end
end
Then place your static pages in app/views/pages/{name}.html.erb (or whatever template format.)
For more you can create static pages using Jekyll bootstrap or also Jekyll using Danger blog
Refer it is very helpful.
An adequate answer to your question would basically look like an introduction to the Rails framework: the MVC structure, templating, and routing DSL at least. Jeff has given a good stab, but his answer still assumes a lot of basic Rails knowledge on your part.
I'd suggest though, that if your webapp is really that simple, Rails might be overkill. I'd look into something lighter, like Sinatra, which has a much lower learning curve than Rails and does a great job of this kind of thing without having to deal with complex routing, magic MVC action/template mapping, etc.
I'd suggest adding your pages in the public folder so as to be served directly without having to pass through rails at all. I'm not an expert though so I'm not sure if this could have any cons if the page is static.
참고URL : https://stackoverflow.com/questions/4479233/static-pages-in-ruby-on-rails
'Nice programing' 카테고리의 다른 글
Java 필터에서 요청 URL을 얻으려면 어떻게해야합니까? (0) | 2020.10.18 |
---|---|
다른 개체를 사용할 때 템플릿 전문화의 다중 정의 (0) | 2020.10.18 |
내 MVC 애플리케이션에 대한 서비스 계층을 생성합니까? (0) | 2020.10.18 |
uitabbarcontroller를 숨기는 방법 (0) | 2020.10.17 |
MVC 유효성 검사에 대한 단위 테스트 (0) | 2020.10.17 |