Skip to content

Instantly share code, notes, and snippets.

@hoasung01
Created April 18, 2014 11:05
Show Gist options
  • Save hoasung01/11038042 to your computer and use it in GitHub Desktop.
Save hoasung01/11038042 to your computer and use it in GitHub Desktop.
##############################
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
@hnq90
Copy link

hnq90 commented May 28, 2014

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:

  1. Lỗi thứ nhất là do attr_accessible chỉ chấp nhận 2 attribute là post_idcart_id
  2. Do không có attribute title.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment