Question

There doesn't appear to be a gem for this, and I think a CMS is overkill as the client only wants to edit the welcome message on the home page!

Here's what I think I should do:

1) Create Page model:

rails g model Page name:string

2) Create Field model:

rails g model Field name:string content:string page_id:integer

3) Create relationship, Page h1:b2 Field

4) Create rake task to set up the message field that belongs to the welcome page:

namespace :seeder do
    namespace :initial_seed do
        task pages: :environment do
            p = Page.create(name: "Welcome")
            p.fields.create(name: "welcomemessage", content: "everything goes here. The long rambling welcome!")
        end
    end
end

5) Create a 'static' controller for the 'static'-ish pages. The home, the about us etc...

class Static < ApplicationController
    def home
        @fields = Page.where().fields
    end
end

6) In the view, populate the welcome message from the database (I'll create a helper for this):

<% field = @fields.find {|x| x[:name] == 'welcomemessage' } %>
<%= field.content %> 

So that's the reading done. Now onto the creation, updation and deletion:

6) Create a control panel controller:

class Panel < ApplicationController
    def pages
        @pages = Page.all
    end
end

7) Display fields in the view at panel/pages.html.erb: (I'll use partials here)

<% @pages.each do |page| %>
    Title: <%= page.name %>
    <% page.fields.each do |field|%>
        Field: <%= field.name %>

        <% form_for(field) do |f| %>
            <% f.text_area :content%>
            <% f.submit %>
        <%= end %>
    <% end %>
<% end %>

Now this is just a rough run down of what I want to do. There are a few problems I want to query, though.

  1. Is this sort of how you would do this?
  2. How should I configure my routes? What is a clever way of populating the @fields variable (see step 5) with the fields for the page we're viewing?
  3. If I do have a panel/pages.html.erb view, should it simply display all of the editable fields in text areas? How should it update these areas? Multiple submit buttons inside multiple forms? What if someone wants to edit many fields at once and submit them all at once?
  4. Where should these forms go? Should I create multiple RESTful actions all inside the Panel controller like this?:

class Panel < ApplicationController
    # new and create not present as the pages have to be created manually
    # Enabling the user to create their own pages with their own layouts is a bit insane
    def pages
        @pages = Page.all
    end
    def pages_update
    end
    def pages_destroy
    end
end

Multiple restful routes in one controller doesn't strike me as organised, but it would make it easier to lock down the panel controller with a before_action hook to redirect if not admin...


Also, I'm nearing the end of a big job, and all I need to do is add the ability to edit one field on one page and them I'm done and I really don't want to have to figure out alchemy_cms or whatever. In future, yes, but, please, please, please someone give me some small pointers here.

No correct solution

OTHER TIPS

I would strongly advise against building your own CMS. It's fraught with difficulties, and it seems like you're running up against some of those now. You should go and check out something like AlchemyCMS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top