Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 4 Strong Params has_many with JSON
    text
    copied!<p>I'm attempting to pass json up on the client side and have rails take care of handling the object creation.</p> <p>Here are my models:</p> <pre><code>class Order &lt; ActiveRecord::Base has_many :order_items, :autosave =&gt; true belongs_to :menu_session end class OrderItem &lt; ActiveRecord::Base belongs_to :order has_one :menu_item end </code></pre> <p>Controller</p> <pre><code>class OrderController &lt; ApplicationController #POST /order/create def create @order = Order.new(order_params) @order.save end private def order_params params.require(:order).permit(:comments, :menu_session_id, :order_items =&gt; [:menu_item_id]) end end </code></pre> <p>The json data:</p> <pre><code>{'order': {'comments': 'none', 'menu_session_id': '9', 'order_items':[{'menu_item_id': '5'}, {'menu_item_id': '5'}]}}; </code></pre> <p>The javascript</p> <pre><code>var data = {}; data.order = {'comments': 'none', 'menu_session_id': '9', 'order_items':[{'menu_item_id': '5'}, {'menu_item_id': '5'}]}; $.post('http://localhost:3000/order/create', orders, function(){}, 'json'); </code></pre> <p>Finally, the error log:</p> <pre><code>Started POST "/order/create" for 127.0.0.1 at 2013-07-10 22:30:36 -0400 Processing by OrderController#create as JSON Parameters: {"order"=&gt;{"comments"=&gt;"none", "menu_session_id"=&gt;"9", "order_items"=&gt;{"0"=&gt;{"menu_item_id"=&gt;"5"}, "1"=&gt;{"menu_item_id"=&gt;"5"}}}} Completed 500 Internal Server Error in 52ms ActiveRecord::AssociationTypeMismatch (OrderItem(#28109220) expected, got Array(#16050620)): app/controllers/order_controller.rb:5:in `create' </code></pre> <p>Clearly, either my json is messed up or the ruby .permit is wrong. However, I've been playing with variations of this for a while now and cannot get it to work. The official documentation doesn't seem to venture into this, and every example I have found here deals with forms. </p> <p>Anyone have any idea what is going on? I can't be the first to try this approach.</p> <hr> <p><strong>UPDATE:</strong></p> <p>Worked around it by making the following changes:</p> <pre><code>class OrderController &lt; ApplicationController #POST /order/create def create @order = Order.new(order_params) order_items = order_item_params order_items.each do |item| @order.order_items &lt;&lt; OrderItem.new(menu_item_id: item) end @order.save end private def order_params params.require(:order).permit(:comments, :menu_session_id) end def order_item_params params.require(:order_items) end end </code></pre> <p>json: <code>{"order":{"comments":"none","menu_session_id":"9"},"order_items":["5","5"]}</code></p> <p>I don't think this would be the best way to do it, so I'm going to leave the question unanswered for now in hopes there is a best practice.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload