Created
April 18, 2014 11:05
-
-
Save hoasung01/11038042 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
############################## | |
class Cart < ActiveRecord::Base | |
has_many :line_items, :dependent => :destroy | |
end | |
class Post < ActiveRecord::Base | |
has_many :line_items | |
end | |
class LineItem < ActiveRecord::Base | |
attr_accessible :cart_id, :post_id | |
belongs_to :cart | |
belongs_to :post | |
end | |
############################## | |
def create | |
@cart = current_cart | |
post = Post.find(params[:post_id]) | |
@line_item = @cart.line_items.build(:post=> post) | |
end | |
# hàm trên mình trích ra từ quyển Agile Web Developement | |
- Cho mình xin hỏi về đoạn | |
@line_item = @cart.line_items.build(:post=> post). Để như thế này thì chạy lỗi nhưng sửa lại như thế này thì ok | |
@line_item = @cart.line_items.build(:post_id=> post) | |
- Ai cho mình lời giải thích với? | |
############################## | |
- Sau khi sửa hàm trên chạy ok rồi thì qua View lại bị lỗi | |
# đoạn code views trên phải là như thế này | |
<ul> | |
<% @cart.line_items.each do |item| %> | |
<li><%= item.post.title %></li> | |
<% end %> | |
</ul> | |
# đoạn này cũng trích ra từ ebook | |
- lỗi là undefined method title |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Không biết bạn đã nhận được câu trả lời chưa nhưng mình thấy thế này:
attr_accessible
chỉ chấp nhận 2 attribute làpost_id
vàcart_id
title
.