我想为我刚刚通过 .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);
}
});
});
};
我知道路线并创建操作,因为模型实例已成功创建,所以知道为什么数据返回未定义?