【发布时间】:2016-01-31 01:30:10
【问题描述】:
我的理解是,如果您指定了正确的内容标头,Rails 将自动以它期望的格式构建 params 散列。但是,我的参数哈希有一些......嗯......问题。这里有一些信息
Angular 2 http 请求
onSubmit(values:any) : void {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http.post("http://localhost:3000/people/",JSON.stringify(values),{ headers: headers })
.subscribe((res: Response) => {
this.data = res.json();
this.loading = true;
});
}
Rails 接收请求,但请求数据的格式不正确。这是我的 rails create 方法。
def create
@person = Person.new.(params[:person])
puts params[:person] # writes to Console
if @person.save
render json: @person
end
end
这是来自我的 puts 语句的 rails 调试器的信息
Parameters: {"name"=>"Christopher", "age"=>"37", "person"=>{"name"=>"Christopher", "age"=>"37"}}
还有剩下的错误:
Completed 500 Internal Server Error in 0ms (ActiveRecord: 0.0ms)
NoMethodError (undefined method `call' for #<Person:0x007fec515761c0>):
app/controllers/people_controller.rb:16:in `create'
除了在周六晚上编程之外,我做错了什么?
【问题讨论】:
-
我不熟悉 Ruby,但这不是 angular2 问题。检查这个answer。
标签: ruby-on-rails angularjs json angular