How to make a page through the admin panel?

There is a documents resource and an appropriate controller:

class DocumentsController < ApplicationController def index @documents = Document.all.paginate(page: params[:page], per_page: 10) end def admin_index @documents = Document.all.paginate(page: params[:page], per_page: 10) render layout: "admin" end def show @document = Document.find(params[:id]) end def admin_show @document = Document.find(params[:id]) render layout: "admin" end .... .... end 

There are 2 layouts:

 application.html.erb, admin.html.erb 

The index controller displays a list of documents in the public part of the site (application.html.erb). The admin_index controller displays a list of documents in a closed part of the site (admin.html.erb).

in the public part of the site I can view any document by clicking on the 'show' link:

 <% @documents.each do |document| %> <%= document.title %> <%= link_to 'Show', document %> <% end %> 

the problem is that in the closed part of the site I cannot see any document by clicking the link:

 <%= link_to 'Show', document %> 

It throws me to the page of a specific document, but layout: application.html.erb, and I need layout: admin.html.erb

routes:

 Testpager::Application.routes.draw do get "admin/index" resources :news, only: [:index, :show] resources :documents, only: [:index, :show, :destroy] get "contacts/index" get "services/index" get "index/index" get "admin/index" get "admin/documents" => 'documents#admin_index' get "admin/documents/:id" => 'documents#admin_show' root 'index#index' end 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

3 answers 3

I would recommend to split the current controller into two (app / controllers / documents_controller.rb and app / controllers / admin / documents_controller.rb). This approach will give several advantages - an isolated namespace for the admin part, independent paths, the ability to set the layout for each controller:

 # app/controllers/admin/documents_controller.rb class Admin::DocumentsController < ApplicationController layout 'admin' def index end end # app/controllers/documents_controller.rb class DocumentsController < ApplicationController def index end end # routes Testpager::Application.routes.draw do namespace :admin do resources :documents, only: [:index, :show, :destroy] end resources :documents, only: [:index, :show, :destroy] end # links link_to "Show in public part", document_path(@document) link_to "Show in admin part", admin_document_path(@document) 

    Try this:

     link_to "Show", controller: "documents", action: "admin_show", id: @document 

    In general, read the documentation for the helper .

    • so tried. This is what I received: No route matches {: action => "admin_show",: controller => "documents" ,: id => nil} - prozaik
    • @prozaik, judging by nil, you have problems with exposed variables - etki

    Great article: http://habrahabr.ru/post/136461/ . For more than 2 years I have been doing the same in the Hierarchy of controllers and have not complained yet.