-
-
Save dvoryankin/fd8becb774b8dac81b3f6119cce1b0df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# BasketsController < ApplicationController | |
class BasketController | |
# всё сразу в одном методе | |
def add_to_basket | |
# могут быть одинаковые названия, поэтому искать лучше по id товара | |
item = Item.find_by_title(params[:item_title]) | |
# непонятно зачем глобальная переменная | |
basket = $basket | |
# проверку количества айтемов можно убрать в валидацию модели | |
# и здесь добавление айтема не внутри условия, так что добавится в любом случае, | |
# но при количестве больше десяти, выведется сообщение | |
basket.add_item(item) | |
if basket.count_items > 10 | |
basket.error_messages.add("Слишком много товаров в корзине") | |
end | |
# вычисление общей суммы можно убрать в модель, в отдельный метод | |
total = 0 | |
basket.items.each do |basket_item| | |
total += basket_item.price | |
end | |
# вместо total в сессию лучше сохранять id корзины | |
session[:basket_total] = total | |
# можно использовать методы ActiveRecord, а не чистый sql | |
query = "INSERT INTO basket_items (basket_id, item_title, basket_type) VALUES '" + | |
(:basket=>basket_id.to_s) + | |
"', '" + params[:item_title] + "', 3)" | |
results = ActiveRecord::Base.connection.execute(query) | |
# бесплатную доставку убрать в модель корзины | |
Basket.add_free_delivery(basket) | |
# сообщение можно выводить через алерты или рендерить паршиал | |
# переменная @item не определена | |
return "<h1>Добавлен товар в корзину: #{@item.title}</h1>" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment