레일즈와 함께하는 애자일 웹개발 4/E
1. p29
<%= link_to "Goodbye", say_goodbye_path %>!
- 괄호가 생략 link_to("Goodbye", say_goodbye_path)로도 가능
- say_goodbye_path에서 /say/goodbye로 변환됨. say가 controller, goodbye가 action
goodbye_say_path는 에러발생함.
2. p75
<%= form_for(@product) do |f| %>
....
<%= f.label :title %>
<%=f.text_field :title %>
- 괄호가 생략
- symbol을 string으로 바꾸어도 동작됨
- :title이 실제 Product 모델이 가지고있는 필드여야함
- form_for에서 @product가 빠지면 에러
3. p81
<%= link_to 'New product', new_product_path %>
- new_product_path가 /products/new로 변환됨
- 1번과는 변환 패턴이 다름. 순서 바꾸면 에러남. 왜????
4. p98
title: products(:ruby)
- fixture 사용시 symbol을 string으로 바꾸어도 동작함
5. p98
assert_equal I18n.translate('activerecord.errors.messages.taken'), product.errors[:title].join('; ')
- 위 구문에서 에러 발생함
- 아래의 구문으로 교체
assert_equal [I18n.translate('errors.messages.taken')], product.errors[:title]
6. p137
def show...
rescue 처리..
end
- rails4에서는 다르게 처리가 가능하다.
- 컨트롤러 상단에 아래의 구문선언
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
- 전역적으로 발생하는 에러정의 후 with:에 선언된 메서드로 처리한다.
- invalid_cart 메서드 추가
def invalid_cart
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to store_url, notice: 'Invalid cart'
end
7. p187
1) product.rb
- has_many :line_items
has_many :orders, through: :line_items
2) products_controller.rb
- @product = Product.find(:params[:id])
3) who_bought.atom.builder
- lastest_order = @product.orders.sort_by(&:updated_at).last
4) order.line_items.each do |item|
- product의 관계 설정은 위와 같이 되어있음
- 2)에 의해서 아래의 쿼리가 실행됨
Product Load (0.2ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 207281427 LIMIT 1
- 3)에 의해서 아래의 쿼리가 실행됨
SELECT `orders`.* FROM `orders` INNER JOIN `line_items` ON `orders`.`id` = `line_items`.`order_id` WHERE `line_items`.`product_id` = 207281427
- through 설정을 통해서 orders와 line_items의 join으로 orders를 가져오는것 같음
- sort_by는 쿼리에 어떻게 표현이 되는거지?
- 4)에 의해서 아래의 쿼리가 실행 (각각의 each에 의해서)
LineItem Load (0.3ms) SELECT `line_items`.* FROM `line_items` WHERE `line_items`.`order_id` = 1
Product Load (0.4ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 207281426 ORDER BY `products`.`id` ASC LIMIT 1
Product Load (0.4ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 207281427 ORDER BY `products`.`id` ASC LIMIT 1
- through 선언이 없으면 아래의 쿼리에러가 발생한다.
Unknown column 'orders.product_id' in 'where clause': SELECT `orders`.* FROM `orders` WHERE `orders`.`product_id` = 207281427
- 본래 위의 예제처럼 line_items의 product_id에 값을 셋팅하고, 이를 통해 order_id로 join이 걸려야하나 through가 없으므로 has_many의 orders 테이블에 product_id를 바로 사용하려고 하다가 에러가 발생함
8. P102
routes.rb
root to: 'store#index', as: 'store'
- as 'store'로 store_path 변수가 생성됨. root to라서 '/'로 생성되는듯함
- as 'store2'로 바꾸면 store2_path 변수로 생성됨
9. path의 생성
- routes.rb에서 resources로 등록이 되어야 controller + action의 path가 생성된다.
10. P182
@order = Order.new(params[:order]);
- 위 구문으로도 어플리케이션은 작동하지만, 나중에 통합테스트에서 에러가 발생한다. (ActiveModel::ForbiddenAttributesError)
- 확인해보니 rails4에서는 strong parameter라고 하여 사용할 파라메터를 화이트리스트로 선언하는 기능이 추가된것 같다. (http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters)
- orders_controller.rb 하단에 보면 order_params이라고 선언된 부분이 있다.
- 위 구문은 @order = Order.new(order_params); 이라고 수정해야한다.
11. P175
redirect_to store_url, notice: '~~~~~'
return
- return redirect_to store_url, notice: '~~~~' 도 작동함
12. P189
get :who_bought, :on => :member
- :on => :member가 없으면 404 에러가 발생
Processing by ProductsController#who_bought as ATOM
Parameters: {"product_id"=>"207281426"}
Completed 404 Not Found in 0ms
- 정확히 무슨 뜻인가...?