Rails3 ActiveResource Post Call delivers an empty Parameter Hash
I am trying to create a new "Person" in a Sinatra API app from a Rails3 app开发者_运维技巧 using ActiveResource and Json. In Rails3, I created a "Person" model and using ActiveResource I correctly call the API, which correctly reads the URL, but no parameters seem to get passed with the object.
From Rails3 Person Model:
class Person < ActiveResource::Base
self.site = "http://127.0.0.1:9393/"
self.collection_name = "person/add"
self.format = :json
end
From the Rails3 console:
u=Person.new({"last_name"=>"Bill", "first_name"=>"Smith"})
=> #<Person:0xb73176f0 @attributes={"last_name"=>"Bill", "first_name"=>"Smith"}, @prefix_options={}>
puts u.attributes
=> last_nameBillfirst_nameSmith
u.save
=> True
From the Sinatra app:
puts @app.params.keys
=> Nil
puts @app.params['last_name']
=> Nil
puts @app.params[:last_name]
=> Nil
Using the IRB Console this works:
Net::HTTP.post_form(URI.parse('http://127.0.0.1:9393/user/add.json'),{'first_name' => 'Smith', 'last_name' => 'Bill'})
Can someone please give some direction as to what I missed or am doing wrong thank you.
Person object should know attributes, as you did on console. When doing Person.find, it gets attrs via activeresource, but Person.new doesn't know them so that any-way to tell to Person is required at Person.new like the following:
class PeopleController < ApplicationController
...
def new
@person = Person.new(:name=>nil, :age=>nil, ...)
end
...
Does this answer?
精彩评论