How can I build my own CMS into a Rails 3.2 app using rails_admin, etc?
admin, content-management-system, ruby-on-rails-3
Solution
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.
Problem
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!