我想通过.ajax()回调函数返回我刚刚创建的对象的JSON数据,但它一直未定义,这让我相信我在创建操作中做错了。这是:
def create
ajax_title = params[:title]
ajax_content = params[:content]
ajax_folder = params[:folder_id]
ajax_parent = params[:parent_id]
ajax_children = Array(params[:children])
@submission = Submission.find_by_title(ajax_title)
if ajax_title && @submission
@submission.content = ajax_content
@submission.save
else
@submission = Submission.create(
:title=>ajax_title,
:content=>ajax_content,
:user_id=>current_user.id,
:folder_id=>ajax_folder,
:parent_id=>ajax_parent,
:children=>ajax_children,
)
end
respond_to do |format|
if @submission.save
format.json { render :json => @submission.to_json }
else
format.json { :errors => @submission.errors }
end
end
end
在底部你会看到我试图渲染它的位置。这是我的AJAX PUT请求:
entryAJAX = function(){
$("#entry-button").click(function(){
$.ajax({
url: "/submissions",
type: "POST",
dataType: "json",
data: {folder_id: <%= @folder.id %>},
complete: function(data){
console.log(data.id);
}
});
});
};
我知道路由并创建动作,因为模型实例已成功创建,所以任何想法为什么数据都不会被定义?