M
M
Michael2018-12-20 15:12:21
Ruby on Rails
Michael, 2018-12-20 15:12:21

Why is the content of the rails view not being displayed?

Layouts
app/views/layouts/admin.html.erb:

<!DOCTYPE html>
<html>
  <head>
    <title>Shop</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag    'admin', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'admin', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
   <%= render "shared/main_menu" %>
  </body>
</html>

Controllers
app/controllers/admin/base_controller.rb
class Admin::BaseController < ApplicationController
  layout 'admin'
end

app/controllers/admin/text_zones_controller.rb
class Admin::TextZonesController < Admin::BaseController
  before_action :set_text_zone, only: [:show, :edit, :create, :update]

  def index
    @text_zones = TextZone.all
  end

  def show
  end

  def new
    @text_zone = TextZone.new
  end

  def edit
  end

  def create
    if @text_zone.save
      redirect_to edit_text_zone_path(@text_zone), notice: 'Текстовая зона добавлена.'
    else
      render action: :new
    end
  end

  def update
    if @text_zone.update_attributes(params[:text_zone])
      redirect_to edit_text_zone_path(@text_zone), notice: 'Текстовая зона успешно обновлена'
    else
      render action: :edit
    end
  end

  def destroy
    @text_zone.destroy
    redirect_to text_zone_index_path, notice: 'Текстовая зона успешно удалена'
  end

private

  def set_text_zone
  @text_zone = TextZone.find(params[:id])
  end
end

app/views/admin/text_zones/index.html.erb
<h1>Текстовые зоны</h1>

<div class="container">
  <div class="row">
    <div class="col-sm-4">
      <h3>Название</h3>
      <p>Lorem ipsum dolor..</p>
      <p>Ut enim ad..</p>
    </div>
    <div class="col-sm-4">
      <h3>Отображается</h3>
      <p>Lorem ipsum dolor..</p>
      <p>Ut enim ad..</p>
    </div>
    <div class="col-sm-4">
      <h3>Описание</h3> 
      <p>Lorem ipsum dolor..</p>
      <p>Ut enim ad..</p>
    </div>
  </div>
</div>

routes.rb
Rails.application.routes.draw do

  root to: "application#index"

  namespace :admin do
  	root to: 'main#index'
  	get 'main/index'
  	
  	resources :text_zones
  end
end

Everything seems to be written in the controller and the model was generated via rails g, the console shows:
HtVEHk-NR6OuLfEwM7UzZQ.png
When you go to the localhost:3000/admin/text_zones page, only the menu from app/views/shared/main_menu is rendered:
uxKfgBBSSxWdLev4t2Y2bQ.png
and where is all the rest of the content from app/views/admin/text_zones /index.html.erb ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Kolodnik, 2018-12-20
@mirbook

because in admin.html there is no template output from the controller
in app/views/layouts/admin.html.erb, you need to add instead of yield, the content from the template will be rendered

<!DOCTYPE html>
<html>
  <head>
    <title>Shop</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag    'admin', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'admin', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
   <%= render "shared/main_menu" %>
   <%= yield %>
  </body>
</html>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question