M
M
Michael2019-03-09 05:20:39
Ruby on Rails
Michael, 2019-03-09 05:20:39

What is the correct way to write code for a specific nested route?

Good afternoon, I want to display: engines/:brand_id/:id, but how to do it?)
In models I use has_many
has_many :engines;
and belongs to respectively
belongs_to :brand;
there is a separate controllers/admin/brands_controller and controllers/admin/engines_controller
which create in the admin panel
/admin/engines/:id
/admin/brands/:id
a separate controller - controllers/engines_controller.rb

class EnginesController < ApplicationController
  add_breadcrumb "Главная", :root_path
  add_breadcrumb "Двигатели", :engines_path

  def index
    @brands = Brand.all
    @engines = Engine.all
  end

  def show
    @engine = Engine.friendly.find(params[:id])
    add_breadcrumb "#{@engine.name}".html_safe
    set_meta_tags(title: @engine.title,
                     description: @engine.description,
                     keywords: @engine.keywords,)
  end
end

views/engines/index.html.erb
<div class="content row">
                <nav>
                  <ol class="breadcrumb">
                    <li class="breadcrumb-item"><%= render_breadcrumbs :separator => ' -> ' %></li>
                  </ol>
                </nav>                
                <h1>Новости</h1>
                <div class="engines">  
                <% @engines.each do |engine| %>
                  <div class="engines__row row">
                    <div class="engines__image"><%= link_to image_tag(engine.image.url(:thumb)), engine_path(engine.brand,engine)
                     %></div>
                    <div class="engines__text">
                        <h3><%= link_to engine.name, engine_path(engine.brand, engine) %></h3>
                        <div class="date"><%= engine.price %></div>
                        <div class="date"><%= engine.displacement %></div>
                        <div class="date"><%= engine.brand.name %></div>
                        </div>
                  </div>
                <% end %>  
                </div>

views/engines/show.html.erb
<div class="content row">
    <div class="news-page">
                <nav>
                  <ol class="breadcrumb">
                    <li class="breadcrumb-item"><%= render_breadcrumbs :separator => ' -> ' %></li>
                  </ol>
                </nav>

                <div class="news-page__title">
                <h1><%= @engine.name %></h1>
                <h1><%= @engine.brand %></h1>
                <h1><%= @engine.price %></h1>
                </div>

                <div class="news-item__wrapper">
                <div class="inner-page__body">
                   <%= raw @engine.content %>
                </div>
                </div>
                </div>
                </div>

routes.rb
Rails.application.routes.draw do
  mount Ckeditor::Engine => '/ckeditor'
  namespace :admin do
    root to: 'main#index'
    get 'main/index'
    
    resources :text_zones
    resources :news, except: :show
    resources :engines, except: :show
    resources :brands
  end


  root to: "application#index"
  resources :text_zones

  resources :news, only: [:index, :show]

  get 'engines/', to: 'engines#index'
  get 'engines/:brand_id/:id', to: 'engine#show', as: :engine

The views/engines/index.html.erb view is displayed normally, but when you click on a separate engine, it gives an error:
IUA4LZqhQoCDix68IuzpKw.png
id is visible:
r0vlzVapSSetFLGH6U1YLg.png
Tell me what I'm doing wrong, and how best to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kozlov, 2019-03-09
@mirbook

Nested routes are described like this:

resources :brands do
  resources :engines
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question