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
# references_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
текущий вызов корзины в любом месте этого приложения
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
и результат такой: NoMethodError в CartItemsController # create неопределенный метод `cart_items ' для BSON :: ObjectId (' 4e156c78421aa918eb000005 '): BSON :: ObjectId Rails.root: / Пользователи / пользователь / railsworks / rcart
Отслеживание приложения |Framework Trace |Приложение Full Trace / controllers / cart_items_controller.rb: 46: в create'
actionpack (3.1.0.rc4) lib/action_controller/metal/implicit_render.rb:4:in
send_action 'actionpack (3.1.0.rc4) lib / abstract_controller / base.rb: 167: в process_action'
actionpack (3.1.0.rc4) lib/action_controller/metal/rendering.rb:10:in
process_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:in
run _1145394839032970144__process_action_ 2623355606222143076 activesupport _callbacks (3.1.0.rc4) lib / active_support / callback: calls.bb:_run_process_action_callbacks'
activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:81:in
run_callbacks 'actionpack (3.1.0.rc4) lib / abstract_controller / callbacks.rb: 17: в process_action'
actionpack (3.1.0.rc4) lib/action_controller/metal/instrumentation.rb:30:in
блок в process_action' activesupport (3.1.0.rc4) lib / active_support / notifications.rb: 55: вblock in instrument'
activesupport (3.1.0.rc4) lib/active_support/notifications/instrumenter.rb:21:in
instrument 'activesupport (3.1.0.rc4) lib / active_support / notifications.rb: 55: в instrument'
actionpack (3.1.0.rc4) lib/action_controller/metal/instrumentation.rb:29:in
process_action' actionpack (3.1.0.rc4) lib / action_controller / metal / params_wrapper.rb: 202: вprocess_action'
actionpack (3.1.0.rc4) lib/action_controller/metal/rescue.rb:17:in
process_action 'activerecord (3.1.0.rc4) lib / active_record / railties / controller_runtime.rb: 18: в process_action'
actionpack (3.1.0.rc4) lib/abstract_controller/base.rb:121:in
процесс' actionpack (3.1.0.rc4) lib / abstract_controller / render.rb: 45: вprocess'
actionpack (3.1.0.rc4) lib/action_controller/metal.rb:193:in
dispatch 'actionpack (3.1.0.rc4) lib / action_controller / metal / rack_delegation.rb: 14: в dispatch'
actionpack (3.1.0.rc4) lib/action_controller/metal.rb:236:in
блок в действии 'пакет действий (3.1.0.rc4) lib / action_dispatch / routing / route_set.rb: 65: в call'
actionpack (3.1.0.rc4) lib/action_dispatch/routing/route_set.rb:65:in
отправка' пакет действий (3.1.0.rc4) lib / action_dispatch / routing / route_set.rb: 29: в блоке call'
rack-mount (0.8.1) lib/rack/mount/route_set.rb:152:in
при вызове 'mount-mount (0.8.1) lib / rack / mount / code_generation.rb: 93: в block in recognize'
rack-mount (0.8.1) lib/rack/mount/code_generation.rb:75:in
optimized_each' rack-mount (0.8.1) lib / rack / mount / code_generation.rb: 92: в recognize'
rack-mount (0.8.1) lib/rack/mount/route_set.rb:141:in
call 'actionpack (3.1.0.rc4) lib / action_dispatch / routing / route_set.rb: 531: в call'
warden (1.0.4) lib/warden/manager.rb:35:in
блоке ввызов 'warden (1.0.4) lib / warden / manager.rb: 34: in catch'
warden (1.0.4) lib/warden/manager.rb:34:in
call' actionpack (3.1.0.rc4) lib / action_dispatch / middleware / best_standards_support.rb: 17: в call'
rack (1.3.0) lib/rack/etag.rb:23:in
call 'rack (1.3.0) lib / rack / conditionalget.rb: 35: in call'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/head.rb:14:in
call 'actionpack (3.1.0.rc4) lib / action_dispatch / middleware / params_parser.rb: 21: в call'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/flash.rb:243:in
call' rack (1.3.0) lib / rack / session / abstract / id.rb: 195: в context'
rack (1.3.0) lib/rack/session/abstract/id.rb:190:in
call 'actionpack (3.1.0.rc4) lib / action_dispatch / middleware / cookies.rb: 321: в call'
activerecord (3.1.0.rc4) lib/active_record/query_cache.rb:54:in
call'activerecord (3.1.0.rc4) lib / active_record / connection_adapters / abstract / connection_pool.rb: 448: в блоке call'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/callbacks.rb:29:in
в вызове 'activesupport (3.1.0.rc4) lib / active_support / callbacks.rb: 392: в _run_call_callbacks'
activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:81:in
run_callbacks' actionpack (3.1.0.rc4) lib /action_dispatch / middleware / callbacks.rb: 28: в call'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/reloader.rb:68:in
call 'rack (1.3.0) lib / rack / sendfile.rb: 102: в call'
actionpack (3.1.0.rc4) lib/action_dispatch/middleware/remote_ip.rb:48:in
call' actionpack (3.1.0.rc4) lib / action_dispatch /middleware / show_exceptions.rb: 47: в call'
railties (3.1.0.rc4) lib/rails/rack/logger.rb:13:in
call 'rack (1.3.0) lib / rack / metadoverride.rb: 24: в call'
rack (1.3.0) lib/rack/runtime.rb:17:in
call' activesupport (3.1.0.rc4) lib / active_support / cache /стратегия / local_cache.rb: 72: в call'
rack (1.3.0) lib/rack/lock.rb:34:in
вызов 'actionpack (3.1.0.rc4) lib / action_dispatch / middleware / static.rb: 53: в call'
railties (3.1.0.rc4) lib/rails/engine.rb:438:in
вызов' railties (3.1.0.rc4) lib /rails / rack / content_length.rb: 16: в call'
railties (3.1.0.rc4) lib/rails/rack/log_tailer.rb:14:in
call 'rack (1.3.0) lib / rack / handler / webrick.rb: 59: в service'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:111:in
service' /Users/user/.rvm/rubies/ruby-1.9.2-p180 / lib / ruby / 1.9.1 / webrick / httpserver.rb: 70: в run'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:in
блоке в start_thread 'Request
Параметры:
{"authenticity_token"=>" vwNaDytLihv5ev3M2tP5uZBjwR1 / t6ld0iFF1gGwWsw = "," product_id "=>" 4e126c01421aa93347000002 "}Показать дамп сеанса
_csrf_token: "vwNaDytLihv5ev3M2tP5uZBjwR1 / t6ld0iFF1gGwWsw =" cart_id: BSON :: ObjectId ('4e156c78421aa918eb000005') сессия_id_0505 *почему это происходит неопределенным методом `cart_items '???спасибо.