Reputation: 434
please help solve the problem.
a resource documents and the 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
a 2 layouts:
application.html.erb,
admin.html.erb
the controller index lists the documents in the public section (application.html.erb). admin_index controller displays a list of documents in the closed part of the site (admin.html.erb).
in the public part of the site I can look any document by clicking on 'show':
<% @documents.each do |document| %>
<%= document.title %>
<%= link_to 'Show', document %>
<% end %>
The problem is that in a closed part of the site I did not get to see any document by clicking the link below:
<%= link_to 'Show', document %>
a problem that throws me a page of a particular document, but the layout: application.html.erb, and I need a 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
Upvotes: 0
Views: 419
Reputation: 1223
First of all, there's no difference in the link you've created with the link_to
helper in your index.html.erb
and admin_index.html.erb
, so let's address that first.
Change your admin_show
route in your routes.rb
to:
get 'admin/documents/:id', to: 'documents#admin_show', as: 'admin_document'
Now change the link_to
in your admin_index.html.erb
to this:
<%= link_to 'Show', admin_document_path(document) %>
That should do it.
The way you've set up the 'public' and 'admin' parts of your site seem odd. I would personally create an admin
namespace and a separate DocumentsController
in that namespace. You'll have to change your routes.rb
, create an admin controller, view and layout if you want this.
Add the documents resource to the admin namespace instead of the admin routes you've already added:
...
namespace :admin do
resources :documents, only: [:index, :show, :destroy]
end
resources :documents, only: [:index, :show, :destroy]
...
Create a app/controllers/admin/documents_controller.rb
file and move the admin_*
methods from your original controller to that one and also move the admin_*.html.erb
views to app/views/admin/*.html.erb
.
Now, last but not least move your app/views/layouts/admin.html.erb
file to app/views/layouts/admin/application.html.erb
. That should do it.
Note that you've got to use a module in your admin controller:
module Admin
class DocumentsController < ApplicationController
...
end
end
Upvotes: 1