3

I have built a website for a client, but they want to be able to costumize the content of the web page, being pictures and text. I have tried and tried and tried, but I have not been able to get something like RefineryCMS as a plugin to work within my app.

I read somewhere about someone who made an own CMS using rails_admin, devise, ckeditor, kaminari, etc. But how can I make my own CMS/admin-area so that the client can costumize the content without messing with the code or having to learn any code?

I have Ruby 1.9.2 and Rails 3.2.2. Does anyone know how I can do this, make my own CMS to plug into this existing app? Is there a tutorial of some sort or can anyone explain this to me? Thanks in advance!

pesolari
  • 85
  • 1
  • 9
  • How good is your knowledge of rails? There are much smarter ways to get a cms. Doing something in rails without learning any code is impossible. – Denny Mueller May 19 '12 at 14:32
  • My knowledge is okay, but my client just wants to be able the content of the website when necessary. So I need an admin area on the page to make sure my client can change the contents of the page without messing with the code. Do you know how to do this? – pesolari May 19 '12 at 14:50
  • Someone told me about making my own admin-area with scaffolding, but how would that work and would that be safe? – pesolari May 19 '12 at 14:53
  • 1
    if your knowledge was ok, you'd not ask such questions – Said Kaldybaev May 19 '12 at 14:54
  • 2
    Okay then I'm a newbie, but I still need help. Can you give me advice? – pesolari May 19 '12 at 14:56

2 Answers2

13

Rails_admin is very good, but often too much for what some people want. Developing your own admin section is quite easy.

How to create your own RESTful Admin section with Rails 3.2

Create your own Admin section with CRUD for all models, including nested resources using namespaces.

Example - Admin section for a Blog - Models: Post, Comment (nested resource of Post)

I'm assuming you have the basic blog app developed. See http://guides.rubyonrails.org/getting_started.html for how to set all this up.

First step - Create admin section and controller

rails g controller admin/admin

This will generate an empty controller from which all our Admin controllers will inherit from. And it will also create views/admin/admin/index.html.erb which can act as out dashboard.

Create admin controllers

rails g controller admin/posts
rails g controller admin/comments

This will generate an empty Admin::PostsController and an Admin::CommentsController in admin namespace

Step 2 - Add namespace for admin controllers Go to config/routes.rb and add the following

    namespace :admin do
      root to: "admin#index"
      resources :posts do
        resources :comments, :only => [:create, :destroy]
      end
    end

Step 3 - Edit admin views and controllers files to work with admin namespace Now we have everything generated we just need to make it work with admin controller and not with front-end.

Change the inheritance of all Admin::*Controllers.

class Admin::PostsController < ApplicationController => class Admin::PostsController < Admin::AdminController

and

class Admin::CommentsController < ApplicationController => class Admin::CommentsController < Admin::AdminController

copy all templates from app/views/posts to app/views/admin/posts copy all templates from app/views/comments to app/views/admin/comments copy all functions from posts_controller.rb to admin/posts_controller.rb copy all functions from comments_controller.rb to admin/comments_controller.rb

Add a link to views/admin/admin/index.html.erb for each of the models you would like to have Admin CRUD controld for. Eg post.

<%= link_to "Posts", admin_posts_path %>

Edit admin/posts_controller.rb. Change the 3 redirect_to calls to work with admin namespace. Create and Update methods:

redirect_to @post => redirect_to [:admin, @post]

Destroy method:

redirect_to posts_url => redirect_to admin_posts_url

Make similar changes in all templates so that they work with within an admin namespace. You need to make following changes:

post/ _form.html.erb:

form_for(@post) => form_for([:admin, @post])

post/ edit.html.erb, index.html.erb, new.html.erb & show.html.erb find all instances of:

<%= link_to ‘Show’, @post %> => <%= link_to ‘Show’, [:admin, @post] %>

posts_path => admin_posts_path

edit_post_path(@post) => edit_admin_post_path(@post)

new_post_path => new_admin_post_path

<%= render "comments/form" %> =>  <%= render "admin/comments/form" %>

comment/ _comment.html.erb

<%= link_to 'Destroy Comment', [comment.post, comment],... => <%= link_to 'Destroy Comment', [:admin, comment.post, comment],...

comment/ _form.html.erb change:

<%= form_for([@post, @post.comments.build]) do |f| %>=> <%= form_for([:admin, @post, @post.comments.build]) do |f| %>

That’s all. Now you’ll have an /admin dashboard with a link to /admin/posts and CRUD actions available.

Shout-out to http://icebergist.com/posts/restful-admin-namespaced-controller-using-scaffolding. His solution from 2008 is still very relevant, however, there are obviously some differences to Rails 3.2 which this answer address; including nested resources.

Stefan Muntwyler
  • 408
  • 1
  • 4
  • 9
0

So u want rails_admin ...

Install the gem

$ gem 'rails_admin'

do a

$ bundle install

then generate the content

$ rails generate rails_admin:install

then

$ rails server

Now u can go to your admin panel on domain.com/admin Maybe you should browse a bit on https://github.com/sferik/rails_admin. If you have much more question than before you should maybe first think about learning rails. Rails is not that easy to use as upload the content of wordpress.zip put in some db settings and done.

Denny Mueller
  • 3,505
  • 5
  • 36
  • 67
  • That's not what I meant. I know how to install rails_admin, but when I'm in the admin-area, I can't change the content, which is what my client wants. And fyi I do know Rails (I have built several websites using rails), but I do not know this about Rails: CMS/admin – pesolari May 19 '12 at 15:57