skip to Main Content

The problem:
Recently I was asked to make a completely static site “dynamic” in Rails.
The static site has hundreds of pages, each contains links to others, and my task was to integrate the static site into a rails app.

I tried:

iframe. I put the static site under /public and load the site in an iframe on my regular rails page. It works but it is not an acceptable solution:

  1. Hundreds of pages in an iframe is bad for SEO. They just cannot be included in my sitemap.xml and indexed by google.
  2. User cannot access a static page by typing a url in the
    browser.
  3. Since Google Analytics code is in the layout, it obviously cannot track the user behavior in the iframe on page.

Am I missing any rails gems/tricks that can handle this situation?

In Rails, what is the best way to handle a large number of static pages?

2

Answers


  1. I trust there is a solution idea can be like , converting all the static pages into CSV or Excel format document and after that getting data in a rails layout .

    Login or Signup to reply.
  2. In Rails, what is the best way to handle a large number of static pages

    To not have a large number of static pages.

    Seriously, Rails is one of the most robust, dynamic and comprehensive application frameworks in existence, and you want to fill it with static pages?

    You’ll be best making a basic CMS and populating it with markdown:


    #app/models/page.rb
    class Page < ActiveRecord::Base
        #columns id | title | body | created_at | updated_at
        #has_many :categories -> the power of Rails; you can add whatever you want! 
    end
    
    #app/controllers/pages_controller.rb
    class PagesController < ApplicationController
       before_action :find_page, only: [:show]
    
       def index
          @pages = Page.all
       end 
       def show
       end
    
       private
    
       def find_page
          @page = Page.find params[:id]
       end
    end
    

    This will give you the ability to create a simple set of routes:

    #config/routes.rb
    root to: "pages#index"
    resources :pages, only: [:index, :show]
    

    You’d have to populate the database yourself, or if you wanted to create a very simple admin area, I’d strongly recommend using Devise:

    #config/routes.rb
    namespace :admin do
        root to: "pages#index"
        resources :pages, path: "" #-> yoururl.com/admin/...
    end 
    

    This will give you the ability to create a CRUD admin/pages controller:

    #app/controllers/admin/pages_controller.rb
    class PagesController < ApplicationController
       before_action :set_page, only: [:edit, :update, :show, :destroy]
       before_action :authenticate_user!
    
       def index
           @pages = Page.all
       end
    
       def edit
       end
    
       def update
          @page.update page_params
       end
    
       def destroy
          @page.destroy page_params
       end
    
       def show
       end
    
       private
    
       def page_params
         params.require(:page).permit(:title, :body)
       end
    
    end
    

    Then you just have to put your pages into the database, allowing you to call them through your routes. If you want custom routes, use friendly_id to create them.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search