Answer the question
In order to leave comments, you need to log in
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
<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>
<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>
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
Answer the question
In order to leave comments, you need to log in
Nested routes are described like this:
resources :brands do
resources :engines
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question