【问题标题】:user has_many :orders association but user.orders can not be found in the page用户 has_many :orders 关联但在页面中找不到 user.orders
【发布时间】:2014-06-18 06:12:38
【问题描述】:

我的仓库如下所示: 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:in `checkout' 请求

我遇到了一个网页不能引用user.orders的问题。我很确定代码中的 current_user 可以正常工作,并且 model/user.rb 和 model/order.rb 中的声明使 has_one 关联良好。

我在项目中的逻辑是 在 carts/index.html 中使用 carts_controller.rb 上定义的 checkout 来呈现支付信息表单。对于结帐功能,它使用 current_user.orders 来 在 current_user.orders 中建立一个新的订单集合。

但是,current_user.orders 似乎没有退出(也在 Rails 委员会中)!需要吃药解决问题~~~谢谢!!!

【问题讨论】:

  • 你的 current_user 方法是怎么说的?
  • 好的,知道了。您在 order 模型中缺少 end 语句。

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


【解决方案1】:
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

【讨论】:

  • @user3546176 没问题。如果问题已解决,请接受答案并标记有用:) 快乐编程
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 2013-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多