-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solve zad6 #265
base: master
Are you sure you want to change the base?
Solve zad6 #265
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dzięki za rozwiązanie! 🎉 Potrzeba kilku poprawek, ale ogólnie kod wygląda okej 👍
end | ||
def show | ||
@customer = Customer.find(params[:id]) | ||
@products = @customer.products.order(price: :desc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
order(price: :desc)
można przerzucić do modelu jako scope, zgodnie z ostatnim wykładem 😉
class Product < ApplicationRecord | ||
has_many :product_categories | ||
has_many :categories, through: :product_categories | ||
belongs_to :customer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
brak opcji optional: true
sprawia, że nie można stworzyć produktu, który nie jest jeszcze przypisany do żadnego customera 😢
def index | ||
@customers = Customer.all | ||
end | ||
def show |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Zgodnie z zadanie, produkty należące do customera powinny być wyświetlane pod adresem /customers/:customer_id/products
. Teraz jest to /customers/:customer_id
. Zauważ, że ten show
jest trochę mylący. Oczekiwalibysmy, że dostaniemy tutaj profil pojedynczego customera a dostajemy produkty 🤔 Żeby to poprawić należałoby:
- zanestować odpowiednio ścieżki
# config/routes.rb
resources :customers, only: :index do
resources :products, only: :index
end
Taki zapis rzeczywiście tworzy ścieżkę /customers/:customer_id/products
, która odpowiada akcji index
w kontrolerze ProductsController
, czyli wszystko jest na swoim miejscu - CustomersController
zajmuje się customerami a ProductsController
produktami. 😉
</tr> | ||
</thead> | ||
<tbody> | ||
<% @products.each do |product| %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wyświetlanie pojedynczego produktu nadaje się na oddzielny plik, z którego można wielokrotnie skorzystać 😉
@@ -1,3 +1,5 @@ | |||
Rails.application.routes.draw do | |||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | |||
resources :customers | |||
resources :products |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No description provided.