user has_many:orders association,但在页面中找不到user.orders

时间:2014-06-18 06:12:39

标签: ruby-on-rails ruby ruby-on-rails-3

我的回购显示如下: https://github.com/Gtar69/artstore_hw2

错误讯息: /Users/Gtar/project2/artstore-store-v1/app/models/order.rb:23:语法错误,意外的输入结束,期待keyword_end

Extracted source (around line #22):
     def checkout  
        #if !current_user
         @order = current_user.orders.build 
         @info = @order.build_info
        #else 
         # redirect_to carts_path

Rails.root: /Users/Gtar/project2/artstore-store-v1

应用程序跟踪|框架跟踪|完整追踪 app / controllers / carts_controller.rb:22:在`checkout' 请求

我遇到了一个网页无法引用user.orders的问题。我很确定代码中的current_user工作,并且在model / user.rb和model / order.rb中声明了has_one关联。

我在项目中的逻辑是 在carts / index.html中使用carts_controller.rb上定义的checkout来呈现付款信息表单。对于checkout函数,它使用current_user.orders 在current_user.orders中构建新的订单收集。

然而,似乎current_user.orders没有退出(也在rails council中)!!需要一些药来解决问题~~~谢谢!

1 个答案:

答案 0 :(得分:0)

class Order < ActiveRecord::Base

  belongs_to :user
  has_many :items, :class_name => "OrderItem", :dependent => :destroy
  has_one :info, :class_name => "OrderInfo", :dependent => :destroy

  accepts_nested_attributes_for :info


  def build_item_cache_from_cart(cart)
    cart.items.each do |cart_item|
      item = items.build
      item.product_name = cart_item.title
      item.quantity = 1
      item.price = cart_item.price
      item.save
    end
  end

  def calculate_total!(current_cart)
    self.total = current_cart.total_price
    self.save
  end
end