开发者

mongoid has_many relations

this is index.html.erb
            <p>
             <%= number_to_currency(product.price) %>  
             <%= button_to 'Add to Cart', cart_items_path(:product_id => product) %>
            </p>

cart.rb

class Cart
  include Mongoid::Document
  include Mongoid::Timestamps 
  # ref开发者_开发问答erences_many :cart_items, :dependent => :destroy
   has_many :cart_items, :dependent => :destroy , :autosave => true 
  # embeds_many :cart_items

  # accepts_nested_attributes_for :cart_items
  # attr_accessible :cart_items_attributes
end

cart_item.rb

class CartItem
  include Mongoid::Document
  include Mongoid::Timestamps 
  # referenced_in :cart
  # embedded_in :cart
  belongs_to :cart
  belongs_to :product 
  # embedded_in :product  
  field :product_id, :type => Integer
  field :cart_id, :type => Integer
  field :quantity, :type => Integer
  field :price, :type => Float
end

application_controller.rb

...

private

current cart call anywhere in this app

def current_cart

cart = if session[:cart_id]
  Cart.find(session[:cart_id])
else
  Cart.create!
end
   session[:cart_id] = cart.id

end

cart_items.rb

... def create

@cart = current_cart 
product = Product.find(params[:product_id]) 
@cart_item = @cart.cart_items.build(:product => product)
# @cart_item = CartItem.new(params[:product => product])
 # @cart_item = CartItem.new(params[:cart_item])

respond_to do |format|
  if @cart_item.save
    format.html { redirect_to(@cart_item,  notice: 'Cart item was successfully created.') }
    format.json { render json: @cart_item, status: :created, location: @cart_item }
  else
    format.html { render action: "new" }
    format.json { render json: @cart_item.errors, status: :unprocessable_entity }
  end
end

end ....

product.rb

require 'carrierwave/orm/mongoid'
class Product
  include Mongoid::Document
  include Mongoid::Timestamps 
  # include Mongoid::Observing
  # include Mongoid::Tree
  has_many :line_items

  # before_destroy :ensure_not_referenced_by_any_line_item

  field :title, :type => String
  field :description, :type => String
  # field :image, :type => String
  mount_uploader :image, ImageUploader
  field :price, :type => Float
  field :category_id, :type => Integer
  field :stock_level, :type => Integer


  set_callback(:destroy, :before) do |document|
    document.ensure_not_referenced_by_any_line_item
  end

  protected
  def ensure_not_referenced_by_any_line_item        
   if cart_items.empty? 
     return true 
   else
     errors.add(:base, 'Line Items present')
     return false 
    end
   end
end

and the result is this: NoMethodError in CartItemsController#create undefined method `cart_items' for BSON::ObjectId('4e156c78421aa918eb000005'):BSON::ObjectId Rails.root: /Users/user/railsworks/rcart

Application Trace | Framework Trace | Full Trace app/controllers/cart_items_controller.rb:46:in create' actionpack (3.1.0.rc4) lib/action_controller/metal/implicit_render.rb:4:insend_action' actionpack (3.1.0.rc4) lib/abstract_controller/base.rb:167:in process_action' actionpack (3.1.0.rc4) lib/action_controller/metal/rendering.rb:10:inprocess_action' actionpack (3.1.0.rc4) lib/abstract_controller/callbacks.rb:18:in block in process_action' activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:416:inrun_1145394839032970144__process_action_2623355606222143076_callbacks' activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:386:in _run_process_action_callbacks' activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:81:inrun_callbacks' actionpack (3.1.0.rc4) lib/abstract_controller/callbacks.rb:17:in process_action' actionpack (3.1.0.rc4) lib/action_controller/metal/instrumentation.rb:30:inblock in process_action' activesupport (3.1.0.rc4) lib/active_support/notifications.rb:55:in block in instrument' activesupport (3.1.0.rc4) lib/active_support/notifications/instrumenter.rb:21:ininstrument' activesupport (3.1.0.rc4) lib/active_support/notifications.rb:55:in instrument' actionpack (3.1.0.rc4) lib/action_controller/metal/instrumentation.rb:29:inprocess_action' actionpack (3.1.0.rc4) lib/action_controller/metal/params_wrapper.rb:202:in process_action' actionpack (3.1.0.rc4) lib/action_controller/metal/rescue.rb:17:inprocess_action' activerecord (3.1.0.rc4) lib/active_record/railties/controller_runtime.rb:18:in process_action' actionpack (3.1.0.rc4) lib/abstract_controller/base.rb:121:inprocess' actionpack (3.1.0.rc4) lib/abstract_controller/rendering.rb:45:in process' actionpack (3.1.0.rc4) lib/action_controller/metal.rb:193:indispatch' actionpack (3.1.0.rc4) lib/action_controller/metal/rack_delegation.rb:14:in dispatch' actionpack (3.1.0.rc4) lib/action_controller/metal.rb:236:inblock in action' actionpack (3.1.0.rc4) lib/action_dispatch/routing/route_set.rb:65:in call' actionpack (3.1.0.rc4) lib/action_dispatch/routing/route_set.rb:65:indispatch' actionpack (3.1.0.rc4) lib/action_dispatch/routing/route_set.rb:29:in call' rack-mount (0.8.1) lib/rack/mount/route_set.rb:152:inblock in call' rack-mount (0.8.1) lib/rack/mount/code_generation.rb:93:in block in recognize' rack-mount (0.8.1) lib/rack/mount/code_generation.rb:75:inoptimized_each' rack-mount (0.8.1) lib/rack/mount/code_generation.rb:92:in recognize' rack-mount (0.8.1) lib/rack/mount/route_set.rb:141:incall' actionpack (3.1.0.rc4) lib/action_dispatch/routing/route_set.rb:531:in call' warden (1.0.4) lib/warden/manager.rb:35:inblock in call' warden (1.0.4) lib/warden/manager.rb:34:in catch' warden (1.0.4) lib/warden/manager.rb:34:incall' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/best_standards_support.rb:17:in call' rack (1.3.0) lib/rack/etag.rb:23:incall' rack (1.3.0) lib/rack/conditionalget.rb:35:in call' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/head.rb:14:incall' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/params_parser.rb:21:in call' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/flash.rb:243:incall' rack (1.3.0) lib/rack/session/abstract/id.rb:195:in context' rack (1.3.0) lib/rack/session/abstract/id.rb:190:incall' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/cookies.rb:321:in call' activerecord (3.1.0.rc4) lib/active_record/query_cache.rb:54:incall' activerecord (3.1.0.rc4) lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in call' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/callbacks.rb:29:inblock in call' activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:392:in _run_call_callbacks' activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:81:inrun_callbacks' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/callbacks.rb:28:in call' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/reloader.rb:68:incall' rack (1.3.0) lib/rack/sendfile.rb:102:in call' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/remote_ip.rb:48:incall' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/show_exceptions.rb:47:in call' railties (3.1.0.rc4) lib/rails/rack/logger.rb:13:incall' rack (1.3.0) lib/rack/methodoverride.rb:24:in call' rack (1.3.0) lib/rack/runtime.rb:17:incall' activesupport (3.1.0.rc4) lib/active_support/cache/strategy/local_cache.rb:72:in call' rack (1.3.0) lib/rack/lock.rb:34:incall' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/static.rb:53:in call' railties (3.1.0.rc4) lib/rails/engine.rb:438:incall' railties (3.1.0.rc4) lib/rails/rack/content_length.rb:16:in call' railties (3.1.0.rc4) lib/rails/rack/log_tailer.rb:14:incall' rack (1.3.0) lib/rack/handler/webrick.rb:59:in service' /Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:111:inservice' /Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:70:in run' /Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:inblock in start_thread' Request

Parameters:

{"authenticity_token"=>"vwNaDytLihv5ev3M2tP5uZBjwR1/t6ld0iFF1gGwWsw=", "product_id"=>"4e126c01421aa93347000002"} Show session dump

_csrf_token: "vwNaDytLihv5ev3M2tP5uZBjwR1/t6ld0iFF1gGwWsw=" cart_id: BSON::ObjectId('4e156c78421aa918eb000005') session_id: "532c1ca0f48a6fca905a97afba912e05"

i did not get the problem point. why this happening undefined method `cart_items' ??? thanks.


I would need to see the code that is throwing an error to give you a definitive answer as to why the error is being thrown (i.e. the create method), but why don't you try this for your Cart creation code:

def current_cart
  cart = Cart.find_or_create_by(:id => session[:cart_id])
  session[:cart_id] ||= cart.id
  return cart
end

It looks as though your current implementation is not actually returning the cart item upon locating / creating it, but instead is returning the session[:cart_id] variable.

An alternative and somewhat prettier solution might be to split the helper up into two methods:

def current_cart_id
  session[:cart_id] ||= Cart.find_or_create_by(:id => session[:cart_id]).id
end

def current_cart
  @current_cart ||= Cart.find(current_cart_id)
end

Then you can easily access the current_cart object and it's id at any time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜