Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
695 views
in Technique[技术] by (71.8m points)

ruby - Nested object creation with JSON in Rails

How do I pass JSON to a RAILS application so it will created nested child objects in a has_many relationship?

Here's what I have so far:

Two model objects.

class Commute < ActiveRecord::Base
  has_many :locations
  accepts_nested_attributes_for :locations, :allow_destroy => true
end

class Location < ActiveRecord::Base
  belongs_to :commute
end

With Commute, I have a standard controller set up. I'd like to be able to create a Commute object as well as several child Location objects in a single REST call using JSON. I've been trying things like this:

curl -H "Content-Type:application/json" -H "Accept:application/json" 
-d "{"commute":{"minutes":0, 
"startTime":"Wed May 06 22:14:12 EDT 2009", 
"locations":[{"latitude":"40.4220061",
"longitude":"40.4220061"}]}}"  http://localhost:3000/commutes

Or more readable, the JSON is:

{
    "commute": {
        "minutes": 0,
        "startTime": "Wed May 06 22:14:12 EDT 2009",
        "locations": [
            {
                "latitude": "40.4220061",
                "longitude": "40.4220061"
            }
        ]
    }
}

When I execute that, I get this output:

Processing CommutesController#create (for 127.0.0.1 at 2009-05-10 09:48:04) [POST]
  Parameters: {"commute"=>{"minutes"=>0, "locations"=>[{"latitude"=>"40.4220061", "longitude"=>"40.4220061"}], "startTime"=>"Wed May 06 22:14:12 EDT 2009"}}

ActiveRecord::AssociationTypeMismatch (Location(#19300550) expected, got HashWithIndifferentAccess(#2654720)):
  app/controllers/commutes_controller.rb:46:in `new'
  app/controllers/commutes_controller.rb:46:in `create'

It looks like the locations JSON array is being read in, but not interpreted as a Location object.

I can easily change either the client or the server, so the solution could come from either side.

So, does RAILS make this easy for me to do? Or do I need to add in some support for this to my Commute object? Perhaps add a from_json method?

Thanks for any help.


As I've been working through this, one solution that works is to modify my controller. But this doesn't seem the "rails" way of doing it, so please still let me know if there's a better way.

def create
    locations = params[:commute].delete("locations");
    @commute = Commute.new(params[:commute])

    result = @commute.save

    if locations 
      locations.each do |location|
        @commute.locations.create(location)
      end
    end


    respond_to do |format|
      if result
        flash[:notice] = 'Commute was successfully created.'
        format.html { redirect_to(@commute) }
        format.xml  { render :xml => @commute, :status => :created, :location => @commute }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @commute.errors, :status => :unprocessable_entity }
      end
    end
  end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"
            }
        ]
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...