#117 Semi-Static Pages (revised)
There are many ways to handle static pages in a Rails application. Here I show two approaches including one that uses a RESTful style PagesController to keep the content in the database
- Download:
- source codeProject Files in Zip (63.6 KB)
- mp4Full Size H.264 Video (23.3 MB)
- m4vSmaller H.264 Video (10.1 MB)
- webmFull Size VP8 Video (10.6 MB)
- ogvFull Size Theora Video (23.5 MB)
We have created a gem that handles things on the database and is supposed to be used with things like activeadmin, or typus:
https://github.com/helabs/static_content
It is fairly easy to implement, and works just fine. Give it a try.
Nice. I recently did something like this, but I used the friendly_id gem instead for the friendly url stuff. Nice info nonetheless.
+1
I usually use friendly_id gem too.
You have the following comment in the routes:
Indeed the update and destroy actions don't work on those pages. What is the solution?
+1
To remedy this issue, I added two more routes, just after the one added in the tutorial to handle the 'PUT' and 'DELETE' options for update and destroy. I've only played around with it for a little while, but I haven't seen any issues. This is how my routes for the pages look:
EDIT: Just for further clarification, the paths to edit, would be
http://<yourapp>/pages/<page>/edit
, rather thanhttp://<yourapp>/<page>/edit
. I haven't worked around that yet, but haven't really tried either.Thanks for clarifying! Works as you said.
The choice of whether using a cms/dynamic approach or a static
implementation is more a question of how often the content is supposed to change and how the process of this change is implemented, rather than how many static files the project contains.
One should not forget the massive performance difference between
these two approaches.
+1
Any thoughts on nesting pages like this? Specifically, say I want an About page at "/about" and a Jobs page at "/about/jobs". I've tried setting the permalink for the jobs page to be "about/jobs", but Rails doesn't recognize this route.
I found a solution on StackOverflow. By appending
constraints: { id: /.*/ }
to the get route, so it reads:it now recognizes routes to pages with slashes in their permalinks like "about/jobs". I guess Rails has its own constraints (like no slashes) on id's that this overrides.
nice, that works well.
I think the order of the routes is important to make this work properly.
resources :products
get ':id', to: 'pages#show', as: :page
resources :pages, except: :show
root to: 'products#index'
Not sure if I did something wrong or if Rails 4 changed, but I ran through this episode using Rails 4 beta. The link in the view to point to page_path("about") worked fine if I wanted http://site/pages/about as my link but did not change to just http://site/about even after changing the route.
I wound up making that page_path["about"] and it seemed to work correctly. Just dropping this here if anyone else encounters it.
I was incorrect :). page_path("about") always points me to /pages/about and page_path["about"] only works if I'm on the /about page manually, then it shows correctly. Otherwise it doesn't show the path. Shows the root path for some reason. Having fun looking at it though to figure it out.
Processing by PagesController#show as
Parameters: {"id"=>"favicon"}
Page Load (0.2ms) SELECT "pages".* FROM "pages" WHERE "pages"."permalink" = 'favicon' LIMIT 1
Can someone tell me why the get ':id', to: 'pages#show', as: :page route in my route.rb file is passing favicon as an id parameter?
cdcdscds