Итак, у меня есть эти 3 модели: купон, место и категория.Я хочу настроить ассоциации так, чтобы:
- Категория имела как купоны, так и связанные с ней места
- Купон принадлежит нескольким категориям
- Место принадлежит нескольким категориям.
Как бы я реализовал ассоциации?
Что у меня сейчас есть:
Модели
Coupon model
class Coupon < ApplicationRecord
belongs_to :category
end
Место Модель
class Place < ApplicationRecord
belongs_to :category
end
Категория Модель
class Category < ApplicationRecord
has_many :coupons
has_many :places
end
Контроллеры: Контроллер купонов:
class CouponsController < ApplicationController
before_action :find_coupon, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
if params[:category].blank?
@coupons = Coupon.all.order("created_at DESC")
else
@category_id = Category.find_by(name: params[:category]).id
@coupons = Coupon.where(category_id: @category_id).order("created_at DESC")
end
end
def show
end
def new
@coupon = Coupon.new
@categories = Category.all.each do |category|
category
end
end
def create
@coupon = Coupon.new(coupon_params)
if @coupon.save
redirect_to @coupon
else
render 'new'
end
@categories = Category.all.each do |category|
category
end
end
def edit
@categories = Category.all.each do |category|
category
end
end
def update
@categories = Category.all.each do |category|
category
end
if @coupon.update(coupon_params)
redirect_to @coupon
else
render "edit"
end
end
def destroy
if @coupon.destroy
redirect_to root_path
end
end
private
def coupon_params
params.require(:coupon).permit(:title, :category_id, :description, :company)
end
def find_coupon
@coupon = Coupon.find(params[:id])
end
end
Места контроллера:
class PlacesController < ApplicationController
before_action :set_place, only: [:show, :edit, :update, :destroy]
def index
@places = Place.all
end
def show
end
def new
@place = Place.new
@categories = Category.all.each do |category|
category
end
end
def edit
end
def create
@place = Place.new(place_params)
respond_to do |format|
if @place.save
format.html { redirect_to @place, notice: 'Place was successfully created.' }
format.json { render :show, status: :created, location: @place }
else
format.html { render :new }
format.json { render json: @place.errors, status: :unprocessable_entity }
end
end
@categories = Category.all.each do |category|
category
end
end
def update
respond_to do |format|
if @place.update(place_params)
format.html { redirect_to @place, notice: 'Place was successfully updated.' }
format.json { render :show, status: :ok, location: @place }
else
format.html { render :edit }
format.json { render json: @place.errors, status: :unprocessable_entity }
end
end
@categories = Category.all.each do |category|
category
end
end
def destroy
@place.destroy
respond_to do |format|
format.html { redirect_to places_url, notice: 'Place was successfully destroyed.' }
format.json { head :no_content }
end
@categories = Category.all.each do |category|
category
end
end
private
def set_place
@place = Place.find(params[:id])
end
def place_params
params.require(:place).permit(:name, :category_id, :description, :address1, :address2, :city, :region, :phone, :email)
end
end
Просмотры: _sidebar частичное:
#sidebar-wrapper
%ul.sidebar-nav
%li.sidebar-brand
%a{:href => root_path}
Shopperbait
%li
- @Category.all.each do |category|
= link_to category.name, coupons_path(category: category.name)
application.html.haml:
!!!
%html
%head
%title ""
%meta{:charset => "utf-8"}/
%meta{:content => "width=device-width, initial-scale=1, shrink-to-fit=no", :name => "viewport"}/
%meta{:content => "ie=edge", "http-equiv" => "x-ua-compatible"}
/ Font Awesome
%link{:crossorigin => "anonymous", :href => "https://use.fontawesome.com/releases/v5.4.2/css/all.css", :integrity => "sha384-/rXc/GQVaYpyDdyxK+ecHPVYJSN9bmVFBvjA/9eOB+pb3F2w2N6fc5qB9Ew5yIns", :rel => "stylesheet"}
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload'
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
= csrf_meta_tags
%body
= render 'shared/navbar'
= render 'shared/sidebar'
схема:
ActiveRecord::Schema.define(version: 20181129131201) do
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "coupons", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "company"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
end
create_table "places", force: :cascade do |t|
t.string "name"
t.text "description"
t.string "address1"
t.string "address2"
t.string "city"
t.string "region"
t.string "phone"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
end
end
Кроме того, я хотел бы иметь возможность показывать ссылки на категории на боковой панели.Какой будет код для просмотров?моя боковая панель не работает