我有一个 json 对象。我想通过传递这个值来创建一个新学生。如何在 ruby on rails 中使用 ajax 将该对象的值传递给控制器?这是我用于传递对象的代码。
self.save = function() {
var dataToSave = {
Name: self.firstName(),
_age: self.Age()
}
alert(JSON.stringify(dataToSave))
$.ajax({
url: '/students/new',
dataType: 'json',
type: 'PUT',
data: {total_changes: JSON.stringify(dataToSave)},
success: function(data) {
alert("Successful");
},
failure: function() {
alert("Unsuccessful");
}
});
// TODO send request
};
}
i 终端有一些错误。表明
Parameters: {"id"=>"new", "total_changes"=>"{\"Name\":\"hu\",\"_age\":\"7\"}"}
id 被视为 new.Rake 路由
[nithinv@ast297 jquery_country]$ rake routes
students GET /students(.:format) students#index
POST /students(.:format) students#create
new_student GET /students/new(.:format) students#new
edit_student GET /students/:id/edit(.:format) students#edit
student GET /students/:id(.:format) students#show
PUT /students/:id(.:format) students#update
DELETE /students/:id(.:format) students#destroy
控制器是
def new
@student = Student.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @student }
end
end
我怎样才能在控制器中创建功能?