Nested object creation with JSON in Rails
Figured it out, the locations object should have been called locations_attributes to match up with the Rails nested object creation naming scheme. After doing that, it works perfectly with a default Rails controller.
{
"commute": {
"minutes": 0,
"startTime": "Wed May 06 22:14:12 EDT 2009",
"locations_attributes": [
{
"latitude": "40.4220061",
"longitude": "40.4220061"
}
]
}
}
Create Nested Object with Rails 4 (JSON call)
Alright, so... I was sending the JSON wrongly:
Instead of:
{
"trip": {
"evnt_acc_red": 3,
"distance": 400
},
"events_attributes": [
{
"distance": 300
},
{
"distance": 400
}
]
}
I should have been sending:
{
"trip": {
"evnt_acc_red": 3,
"distance": 400,
"events_attributes": [
{
"distance": 300
},
{
"distance": 400
}
]
}
}
How to render content from nested json object in ruby on rails view
Shouldn't something like
@post[:heroimage][:url]
or
@post['heroimage']['url']
work?
With ruby, you should be able to treat the keys like a symbol. and ruby won't use dot notation in a hash so you would need to use square brackets.
You don't need to loop on them and create them individually
You don't need to loop on them and create them individually. Plan.new(plan_params)
should do the work, you just need to specify the nested attributes properly.
Changes:
days
today_attributes
- Dont need to pass ids.
For example:
{
"name": "Test Plan",
"monthly_price": 0,
"image_url": null,
"day_attributes": [
{
"number": 1,
"meal_attributes": [
{
"name": "Kale Salad",
"calories": null,
"protein": null,
"fat": null,
"carbohydrates": null,
"image_url": null,
"categorie": "snack-1"
},
{
"name": "Fit Burger",
"calories": null,
"protein": null,
"fat": null,
"carbohydrates": null,
"image_url": null,
"categorie": "meal-1"
},
{
"name": "Vegan Rataouille",
"calories": null,
"protein": null,
"fat": null,
"carbohydrates": null,
"image_url": null,
"categorie": "snack-2"
},
{
"name": "Chicken BBQ",
"calories": null,
"protein": null,
"fat": null,
"carbohydrates": null,
"image_url": null,
"categorie": "meal-3"
}
]
},
{
"number": 2,
"meal_attributes":
[
{
"name": "Woldrof Salad",
"calories": null,
"protein": null,
"fat": null,
"carbohydrates": null,
"image_url": null,
"categorie": "snack-1"
},
{
"name": "Baked Beef",
"calories": null,
"protein": null,
"fat": null,
"carbohydrates": null,
"image_url": null,
"categorie": "meal-1"
}
]
}
]
}
Also you should mention them properly in permit params as well like:
params.permit(:name, :monthly_price, :image_url, day_attributes: [:number, meal_attributes: [:name, :calories, :protein, :fat, :carbohydrates, :image_url, :categorie]])
For more info you can refer to:
Active Record Nested Attributes
Railscasts #196
Rails 4 render json nested objects
I got it.
render :json => @booking, :include => [:paypal,
:boat_people,
:boat => {:only => :name, :include => {:port => {:only => :name, :include => {:city => {:only => :name, :include => {:country => {:only => :name}}}}},
:boat_model => {:only => :name, :include => {:boat_type => {:only => :name}}}}}]
Related Topics
Is There a Way Rails 3.0.X Can Default to Using Thin
Ruby Koan: Constants Become Symbols
Rmagick Remove White Background from Image and Make It Transparent
How to Compile Ruby with Rvm on a Low Memory System
In Ruby, How to Output JSON from Hash and Give It Line Breaks and Tabs
Ruby - Dynamically Add Property to Class (At Runtime)
Rails "Undefined Method for Activerecord_Associations_Collectionproxy"
Dynamic Rails Routes Based on Database Models
Migrating from Rails 2 to Rails 3
What Ide to Use for Developing in Ruby on Rails on Windows
Verify Client Certificate Using Sslserver in Ruby
How to Resolve Rails Model Namespace Collision