我有三个模型Order, order_item
和food_item
order has_many order_items
和order_item has_many food_items
当我从我的order_controller API创建一个新订单时,我收到以下错误,但不确定是什么错误。
NoMethodError Exception: undefined method `order_item' for #<Order:0x00000009dd52a8>
orders_controller.rb
class Api::V1::OrdersController < Api::V1::ApplicationController
protect_from_forgery with: :null_session
skip_before_filter :verify_authenticity_token, :if => Proc.new {|c| c.request.format.json? }
before_action :fetch_restaurent
before_action :fetch_food_items, :only => [:index]
after_action :send_order_to_kitchen, :only => [:create, :update]
def index
@orders = Restaurant.orders.fetch_items_from_ethor
end
def show
# Display the list of food_items available from the restaurant
# Allow the customer to place an order
end
def create
@order = Order.new(order_params)
if @order.save
render :json, order, status:201, location: [:api, current_customer, order]
else
render :json, { errors: order.errors }, status:422
end
end
# once the order is placed just send it to the kitechn()
def update
@order = Order.find_by(params[:id])
@rder.update_attributes(order_params)
if @order.save
render :json, order, status: 201, location: [:api, current_user.order.id]
else
render :json,{errors: order.errors}, status: 422
end
end
private
def order_params
params.require(:order).permit(:customer_id, :order_id, :pos_id, :table_id, :order_number,
:order_status,:order_date, :food_item_ids)
end
end
order.rb#model file
class Order < ActiveRecord::Base
belongs_to :customer
belongs_to :restaurant
belongs_to :table
has_many :order_items
has_many :food_items, through: :order_items
validates :customer_id, :order_number, presence:true
validates :subtotal, :final_total, presence:true, numericality:{ greater_than_or_equal_to:0 }
end
order_item.rb#model
class OrderItem < ActiveRecord::Base
belongs_to :food_item, inverse_of: :order_items
belongs_to :order, inverse_of: :order_items
belongs_to :restaurant
has_many :food_items
end
food_item.rb#model
class FoodItem < ActiveRecord::Base
belongs_to :restaurant
belongs_to :order, inverse_of: :food_items
belongs_to :order_item, inverse_of: :food_items
has_many :categories_food_items
has_many :categories, through: :categories_food_items
end
我不明白它在哪里要求order_item。请帮我理解解决这个问题。
undefined method `order_item' for #<Order:0x00000009dd52a8>
我的堆栈跟踪:
Started POST "/api/v1/orders?access_token=testing&order[customer_id]=1&order[order_number]=101&order[pos_id]=222&order[table_id]=1&order[order_status]=pending&order[order_date]=02/03/2015" for 127.0.0.1 at 2015-06-22 14:39:30 +0530
ActiveRecord::SchemaMigration Load (0.1ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by Api::V1::OrdersController#create as JSON
Parameters: {"access_token"=>"testing", "order"=>{"customer_id"=>"1", "order_number"=>"101", "pos_id"=>"222", "table_id"=>"1", "order_status"=>"pending", "order_date"=>"02/03/2015"}}
ApiKey Load (0.1ms) SELECT `api_keys`.* FROM `api_keys` WHERE `api_keys`.`access_token` = 'testing' LIMIT 1
Restaurant Load (0.1ms) SELECT `restaurants`.* FROM `restaurants` WHERE `restaurants`.`id` IS NULL LIMIT 1
Completed 500 Internal Server Error in 35ms
** [Airbrake] Notice was not sent due to configuration:
Environment Monitored? false
API key set? true
NoMethodError - undefined method `order_items' for #<Class:0x0000000a70f0f8>:
activerecord (4.1.8) lib/active_record/dynamic_matchers.rb:26:in `method_missing'
app/controllers/api/v1/orders_controller.rb:24:in `create'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:215:in `block in halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
searchkick (0.8.7) lib/searchkick/logging.rb:107:in `process_action'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call'
xray-rails (0.1.16) lib/xray/middleware.rb:37:in `call'
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.3) lib/rack/etag.rb:23:in `call'
rack (1.5.3) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.3) lib/rack/head.rb:11:in `call'
remotipart (1.2.1) lib/remotipart/middleware.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.3) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.3) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.8) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
airbrake (4.2.0) lib/airbrake/rails/middleware.rb:13:in `call'
better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.3) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.3) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.3) lib/rack/lock.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.3) lib/rack/sendfile.rb:112:in `call'
airbrake (4.2.0) lib/airbrake/user_informer.rb:16:in `_call'
airbrake (4.2.0) lib/airbrake/user_informer.rb:12:in `call'
railties (4.1.8) lib/rails/engine.rb:514:in `call'
railties (4.1.8) lib/rails/application.rb:144:in `call'
rack (1.5.3) lib/rack/content_length.rb:14:in `call'
thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
thin (1.5.1) lib/thin/connection.rb:54:in `process'
thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.5) lib/eventmachine.rb:187:in `run'
thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
thin (1.5.1) lib/thin/server.rb:159:in `start'
rack (1.5.3) lib/rack/handler/thin.rb:16:in `run'
rack (1.5.3) lib/rack/server.rb:264:in `start'
railties (4.1.8) lib/rails/commands/server.rb:69:in `start'
railties (4.1.8) lib/rails/commands/commands_tasks.rb:81:in `block in server'
railties (4.1.8) lib/rails/commands/commands_tasks.rb:76:in `server'
railties (4.1.8) lib/rails/commands/commands_tasks.rb:40:in `run_command!'
railties (4.1.8) lib/rails/commands.rb:17:in `<top (required)>'
bin/rails:4:in `<main>'
答案 0 :(得分:0)
我弄清楚为什么它会为{`
说undefined method
order_item'
我实际上正在验证订单模型中的order_item。这不应该。
更改我的订单模型如下(删除order_item上的验证)后,问题就解决了。
class Order < ActiveRecord::Base
belongs_to :customer
belongs_to :restaurant
belongs_to :table
has_many :order_items
has_many :food_items, through: :order_items
validates :customer_id, :order_number, presence:true
validates :subtotal, :final_total, presence:true, numericality:{ greater_than_or_equal_to:0 }