0

これは、同じ名前の別の質問と重複していません。

Rails 4.0 と Ruby 2.0.0 を Ember (および Ember-data) で使用しています。

私の JSON はcreate、次の形式で OrdersController のアクションに投稿されます。

Parameters: {"order"=>{"status"=>"pending payment", "date"=>"5/11/2013", ... "user_id"=>11, "booking_id"=>8}}

...JSON で送信される他のプロパティの切り捨てはどこにありますか。

上記のエラーが表示されます: 関連付けのためにユーザー レコードの代わりに Fixnum (整数) を返します。私の注文モデル:

class Order < ActiveRecord::Base
    resourcify
    belongs_to :user
    belongs_to :booking, inverse_of: :orders
end

また、Rails 4 の OrdersController の強力なパラメーター:

# Strong Parameters (Rails 4)
def order_params
  params.require(:order).permit(:status, :date, ... :user_id, :booking_id)
end

ここでも...、他のプロパティの切り捨てです。

型の不一致が原因でエラーが発生していることは理解していますが、理由がわかりません。同一の構文を使用してレコードの関連付けを構築する他のモデルがありますが、型の不一致を返すモデルはありません。

必要な場合はcreate、OrdersController のアクション:

# POST /orders
# POST /orders.json
def create
  @order = Order.create(order_params)
  if @order.new_record?
    render json: { errors: @order.errors.messages }, status: 422
  else
    render json: @order, status: 201
  end
end

助けてください!

編集:リクエストによる完全なエラー(それはモンスターであり、私はHerokuを使用しています):

ActiveRecord::AssociationTypeMismatch (User(#70045315577100) expected, got Fixnum(#70045289133740)):


vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/associations/association.rb:216:in 'raise_on_type_mismatch!'
   vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/associations/belongs_to_association.rb:11:in 'replace'
   vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/associations/singular_association.rb:17:in 'writer'
  vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/associations/builder/association.rb:78:in 'user='
   vendor/bundle/ruby/2.0.0/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:92:in 'block in assign_attributes'
   vendor/bundle/ruby/2.0.0/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:91:in 'each'
   vendor/bundle/ruby/2.0.0/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:91:in 'assign_attributes'
   vendor/bundle/ruby/2.0.0/gems/cancan-1.6.10/lib/cancan/inherited_resource.rb:10:in 'load_resource_instance'
   vendor/bundle/ruby/2.0.0/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:32:in 'load_resource'
   vendor/bundle/ruby/2.0.0/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:25:in 'load_and_authorize_resource'
   vendor/bundle/ruby/2.0.0/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:10:in 'block in add_before_filter'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:397:in '_run__1255486436321517885__process_action__callbacks'
   vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in 'run_callbacks'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/abstract_controller/callbacks.rb:17:in 'process_action'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_controller/metal/rescue.rb:29:in 'process_action'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_controller/metal/instrumentation.rb:31:in 'block in process_action'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications.rb:159:in 'block in instrument'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in 'instrument'
   vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications.rb:159:in `instrument'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_controller/metal/instrumentation.rb:30:in 'process_action'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_controller/metal/params_wrapper.rb:245:in 'process_action'
   vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/railties/controller_runtime.rb:18:in 'process_action'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/abstract_controller/base.rb:136:in 'process'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/abstract_controller/rendering.rb:44:in 'process'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_controller/metal.rb:195:in 'dispatch'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_controller/metal/rack_delegation.rb:13:in 'dispatch'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_controller/metal.rb:231:in 'block in action'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/routing/route_set.rb:80:in 'call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/routing/route_set.rb:80:in 'dispatch'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/routing/route_set.rb:48:in 'call'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/journey/router.rb:71:in 'block in call'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/journey/router.rb:59:in 'each'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/journey/router.rb:59:in 'call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/routing/route_set.rb:655:in 'call'
  vendor/bundle/ruby/2.0.0/gems/rack-pjax-0.7.0/lib/rack/pjax.rb:12:in 'call'
  vendor/bundle/ruby/2.0.0/gems/newrelic_rpm-3.6.8.168/lib/new_relic/rack/error_collector.rb:43:in 'call'
 vendor/bundle/ruby/2.0.0/gems/newrelic_rpm-3.6.8.168/lib/new_relic/rack/agent_hooks.rb:22:in 'call'
  vendor/bundle/ruby/2.0.0/gems/newrelic_rpm-3.6.8.168/lib/new_relic/rack/browser_monitoring.rb:16:in 'call'
  vendor/bundle/ruby/2.0.0/gems/rack-cors-0.2.8/lib/rack/cors.rb:54:in 'call'
   vendor/bundle/ruby/2.0.0/gems/warden-1.2.3/lib/warden/manager.rb:35:in 'block in call'
   vendor/bundle/ruby/2.0.0/gems/warden-1.2.3/lib/warden/manager.rb:34:in 'catch'
   vendor/bundle/ruby/2.0.0/gems/warden-1.2.3/lib/warden/manager.rb:34:in 'call'
   vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/etag.rb:23:in 'call'
   vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/conditionalget.rb:35:in 'call'
   vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/head.rb:11:in 'call'
2   vendor/bundle/ruby/2.0.0/gems/remotipart-1.2.1/lib/remotipart/middleware.rb:27:in 'call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/params_parser.rb:27:in 'call'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/flash.rb:241:in 'call'
  vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/query_cache.rb:36:in 'call'
   vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in 'call'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/callbacks.rb:29:in 'block in call'
   vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:373:in '_run__2306864034802654393__call__callbacks'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in 'run_callbacks'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/callbacks.rb:27:in 'call'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/remote_ip.rb:76:in 'call'
   vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/debug_exceptions.rb:17:in 'call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/show_exceptions.rb:30:in 'call'
  vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/rack/logger.rb:38:in 'call_app'
  vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/rack/logger.rb:21:in 'block in call'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/tagged_logging.rb:67:in 'block in tagged'
 vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/tagged_logging.rb:25:in 'tagged'
  vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/tagged_logging.rb:67:in 'tagged'
  vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/rack/logger.rb:21:in 'call'
 vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/request_id.rb:21:in 'call'
  vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/runtime.rb:17:in 'call'
   vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/cache/strategy/local_cache.rb:83:in 'call'
  vendor/bundle/ruby/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/static.rb:64:in 'call'
 vendor/bundle/ruby/2.0.0/gems/heroku-deflater-0.5.1/lib/heroku-deflater/skip_binary.rb:19:in 'call'
 vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/deflater.rb:25:in 'call'
 vendor/bundle/ruby/2.0.0/gems/heroku-deflater-0.5.1/lib/heroku-deflater/serve_zipped_assets.rb:49:in 'call'
vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/engine.rb:511:in 'call'
 vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/application.rb:97:in 'call'
 vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:in 'method_missing'
 vendor/bundle/ruby/2.0.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:552:in 'process_client'
  vendor/bundle/ruby/2.0.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:632:in 'worker_loop'
 vendor/bundle/ruby/2.0.0/gems/newrelic_rpm-3.6.8.168/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb:22:in 'call'
  vendor/bundle/ruby/2.0.0/gems/newrelic_rpm-3.6.8.168/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb:22:in 'block (4 levels) in <top (required)>'
  vendor/bundle/ruby/2.0.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:500:in 'spawn_missing_workers'
  vendor/bundle/ruby/2.0.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:142:in 'start'
 vendor/bundle/ruby/2.0.0/gems/unicorn-4.6.3/bin/unicorn:126:in '<top (required)>'
  vendor/bundle/ruby/2.0.0/bin/unicorn:23:in 'load'
 vendor/bundle/ruby/2.0.0/bin/unicorn:23:in '<main>'

フルorder_params:

def order_params
  params.require(:order).permit(:status, :amount, :date, :user_id, :booking_id, :billing_surname, :billing_firstnames, :billing_address, :billing_city, :billing_postcode, :billing_country, :delivery_surname, :delivery_firstnames, :delivery_address, :delivery_city, :delivery_postcode, :delivery_country, :crypt)
end

フルオーダーcreate方法:

# POST /orders
# POST /orders.json
def create
  @order = Order.new(params[:order])

  respond_to do |format|
    if @order.save
      format.html { redirect_to @order, notice: 'Order was successfully created.' }
      format.json { render json: @order, status: :created }
    else
      format.html { render action: "new" }
      format.json { render json: @order.errors, status: :unprocessable_entity }
    end
  end
end
4

0 に答える 0