Creating an administration area in a Rails application

I am creating a blog with Rails, and the first thing I have done is the administration area (now this is the only thing I have in the application). I used Bootstrap to quickly create all pages and for authentication.

For all models, views, and controllers, I used scaffolding , and I created Admin and Post models.

The problem is that now I have to create a REAL blog and access the admin panel using the /admin route. For example, to create a new post, I have to access http:/mysite/admin/posts/new .

Another problem is that I will have a completely different design on the public blog page (and not in Bootstrap), and, of course, I will have different controllers, views and routes.

So what can be done?

+6
source share
2 answers

I would suggest removing the Admin model, as in your case, it looks more like a namespace than a model. Instead, I would create a namespace :admin in your routes.rb , for example:

 namespace :admin do resources :posts end 

This will cause all routes inside this block to have the w / Admin prefix. Thus, the URL for editing the message on the administrator side will be admin/posts/:id/edit .

Next, I would suggest making an AdminController from which all your admin controllers inherit. This way you can specify a new layout. Then you can create Admin::PostsController in app/controllers/admin/posts_controller.rb

application / controllers / admin_controller.rb

 class AdminController < ApplicationController layout 'admin' end 

application / controllers / admin / posts_controller.rb

 class Admin::PostsController < AdminController def index # admin/posts end end 

app / view / admin / posts / index.html.erb

 Hello from the admin/posts view! 
+15
source

I believe that the blog page should be publicly accessible, which means that authentication is not required to view it. For the rest, you are already using a device to protect this area.

For different templates, it’s quite simple, you can create 2 layouts and set the desired layout in the controllers.

0
source

Source: https://habr.com/ru/post/944540/


All Articles