Я создаю приложение на торговой площадке, где пользователи могут публиковать музыкальные биты, которые хотят продать, и люди могут их покупать.
Когда пользователю нравится удар, и он хочет добавить удар в корзинуЭто застревает на этом действии создания "@beat = Beat.find (params [: id])" *
class LineItemsController < ApplicationController
def create
@beat = Beat.find(params[:id])
@line_item = @cart.add_beat(beat)
respond_to do |format|
if @line_item.save
format.html { redirect_to @line_item.cart, notice: 'Item added to cart' }
format.json { render :show, status: :created, location: @line_item }
else
format.html { render :new }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
end
Вот моя модель line_item
class LineItem < ApplicationRecord
belongs_to :beat
belongs_to :cart
end
Вот моя корзинамодель
class Cart < ApplicationRecord
has_many :line_items, dependent: :destroy
def add_beat(beat)
current_item = line_items.find_by(beat_id: beat.id)
if current_item
current_item.increment(:quantity)
else
current_item = line_items.build(beat_id: beat.id)
end
current_item
end
end
И последнее, но не менее важное, вот моя модель удара
class Beat < ApplicationRecord
before_destroy :not_referenced_by_any_line_item
belongs_to :user, optional: true
has_one_attached :mp3
has_one_attached :image
has_many :line_items
private
def not_referenced_by_any_line_item
unless line_items.empty?
errors.add(:base, "line items present")
throw :abort
end
end
end
Вот маршруты сейчас
Rails.application.routes.draw do
resources :line_items
resources :carts
root 'pages#index'
get 'users/show'
resources :beats
resources :songs
resources :users
devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout', sign_up: 'register' }
end