0

Здравствуйте. Есть модели Rent и Sell они относятся к моделе Type и имеют type_id (rent и sell belongs_to :type, и type has_many :rents, has_many :sells) у Type есть квартиры, дома, гаражы.. итд, как сделать чтоб, когда я перехожу на страницу Rent и выбираю допустим квартиры, отображались квартиры только с модели Rent (сейчас выводятся квартиры с Rent и Sell)

class RentsController < ApplicationController

before_action :find_rent, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
before_action :require_same_user, only: [:edit, :update]

def index
    @search = Rent.search(params[:q])
    @rents = @search.result
    @types = Type.all
end

def search
    index
    render :index
end

def show
    @types = Type.all
end

def new
    @rent = current_user.rents.build
    @sell = current_user.rents.build
end

def create
    @rent = current_user.rents.build(rent_params)
    if @rent.save
        redirect_to @rent, success: ''
    else
        render :new, danger: ''
    end
end

def edit

end

def update
    if @rent.update(rent_params)
        redirect_to @rent, success: ''
    else
        render :edit, danger: ''
    end
end

private

def find_rent
    @rent = Rent.find_by(slug: params[:id])
end

def rent_params
    params.require(:rent).permit(:city, :full_address, :district, :street, :street_number, :full_address, :description, :price, :room, :sleep_places, :total_sq, :living_sq, :kitchen_sq, :floor, :floors, :constr_year, :currency, :type_id, attachments:[])
end

def require_same_user
    if current_user.id != @rent.user_id 
        flash[:danger]
            redirect_to root_path
        end
    end
end

Модеь Sell такая же но со своими параметрами

И модель Type

class TypesController < ApplicationController

before_action :set_type, only: [ :show, :edit, :update, :destroy]

def index
    @rents = Rent.all
    @sells = Sell.all
    @types = Type.all
end

def show
    @rents = Rent.where(type_id: [@type])
    @sells = Sell.where(type_id: [@type])
    @types = Type.all
end

def new
    @type = Type.new
end

def create
    @type = Type.new(type_params)
    if @type.save
        redirect_to types_path, success: ''
    else
        render :new, danger: ''
    end
end

def edit
end

def update
    if @type.update(type_params)
        redirect_to types_path, success: ''
    else
        render :edit, danger: ''
    end
end

def destroy
    @type.destroy
    redirect_to root_path, success: ''
end

private

def set_type
    @type = Type.find(params[:id])
end

def type_params
    params.require(:type).permit(:name, :image)
end
end

Index Rent и Index Sell такая же

.container
  = render 'types/type_estate'  
 .section-rents
    = render 'layouts/sort-bar'
    = render @rents

Когда я перехожу на index Rent или Sell и выбираю любую из категорий Type (дом или гараж ...) то на show Type выводятся посты (дом, гараж ...) с Rent и Sell а нужно только те которые относятся к даной моделе ну и так же на Rent

 show Type

 .container
  = render 'type_estate'
  / = render 'posts/search-form'
 .section-posts
    / = render @rents
    / = render @sells

ну и роути

Rails.application.routes.draw do
get 'user/users'
resources :user
devise_for :users
resources :types
resources :rents
resources :sells
# resources :sells do 
#   resources :types
# end
resources :homes
root 'homes#home'
end
2
  • И ещё стоит почитать про контроллеры в рельсах, тут, например rusrails.ru/action-controller-overview , сразу найдёте и сможете поправить у себя ошибки.
    – Viktor
    20 ноя 2017 в 22:56
  • очень полезно! по вашему я это не делал?
    – Dom
    21 ноя 2017 в 13:04

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.