Javascript 当新数据提交到数据库时,使用AJAX更新rails应用程序页面
我有一个表位于/索引的应用程序。此表中填写了数据库中的记录。另一个第三方服务对此应用程序进行“发布”并创建新的DB记录。我希望在/index页面上使用AJAX显示此记录,而无需刷新页面 来自第三方服务的新记录以每分钟2条的速度提供。所以我想在我的javascript中使用一个计时器循环(30秒)来实现这一点,该循环将发出get at/get_latest_leads,并定义一个控制器动作get_latest,该动作将在过去30秒内输入/更新记录。然后使用ID(在有重复条目的情况下检查重复条目)。在客户端,我将使用javascript将这些新记录预先挂起到表中 控制器Javascript 当新数据提交到数据库时,使用AJAX更新rails应用程序页面,javascript,ruby-on-rails,ajax,ruby-on-rails-3,Javascript,Ruby On Rails,Ajax,Ruby On Rails 3,我有一个表位于/索引的应用程序。此表中填写了数据库中的记录。另一个第三方服务对此应用程序进行“发布”并创建新的DB记录。我希望在/index页面上使用AJAX显示此记录,而无需刷新页面 来自第三方服务的新记录以每分钟2条的速度提供。所以我想在我的javascript中使用一个计时器循环(30秒)来实现这一点,该循环将发出get at/get_latest_leads,并定义一个控制器动作get_latest,该动作将在过去30秒内输入/更新记录。然后使用ID(在有重复条目的情况下检查重复条目)。
# GET /leads
# GET /leads.json
def index
@leads = Lead.find(:all, :conditions => {:is_valid => true}, :order => "id DESC").paginate(:per_page => 30, :page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.json { render json: @leads }
format.js
end
end
def get_latest
logger.info "in get_latest"
@leads = Lead.where('updated_at > ?', Time.now - 30.seconds)
@leads.each { |lead|
logger.info "#{lead.name}"
}
respond_to do |format|
format.json { render json: @leads }
end
end
routes.rb
get 'get_latest_leads', :to => 'leads#get_latest', as: :get_latest_leads
alert("here1");
setInterval(
function(){
new Ajax.Request('get_latest_leads' + '.json', {
alert("here2");
method: 'get',
onSuccess: function(transport){
var leads = transport.response.responseJSON;
// insert the last checked time into <div id='time'></div>
// $('time').innerHTML = records.time;
// loop over response JSON and do what you want with the data
leads.each(function(lead){
$$('table#leads').insert({
top: '<tr><td>'+lead.attribute+'</td></tr>';
});
});
}
});
},
30000
);
index.js.erb
get 'get_latest_leads', :to => 'leads#get_latest', as: :get_latest_leads
alert("here1");
setInterval(
function(){
new Ajax.Request('get_latest_leads' + '.json', {
alert("here2");
method: 'get',
onSuccess: function(transport){
var leads = transport.response.responseJSON;
// insert the last checked time into <div id='time'></div>
// $('time').innerHTML = records.time;
// loop over response JSON and do what you want with the data
leads.each(function(lead){
$$('table#leads').insert({
top: '<tr><td>'+lead.attribute+'</td></tr>';
});
});
}
});
},
30000
);
警报(“此处1”);
设定间隔(
函数(){
新的Ajax.Request('get_latest_leads'+'.json'{
警惕(“此处2”);
方法:“get”,
onSuccess:功能(传输){
var leads=transport.response.responseJSON;
//将上次检查的时间插入到
//$('time').innerHTML=records.time;
//循环响应JSON并对数据执行所需操作
lead.每个功能(lead){
$$(‘表#引线’)。插入({
顶部:''+lead.attribute+'';
});
});
}
});
},
30000
);
问题是我的js根本没有被调用,在浏览器/服务器端也没有错误。当我从我的浏览器/get_latest_leads.json手动请求时,它会将最新的leads作为json获取。但我的js并没有这么说。你能告诉我出了什么问题吗
更新:
我按照来自的说明,在应用程序树中将我的js相应地移动到assets/javascripts/leads/index.js
这使得呼叫成功,现在我每30秒收到一次警报,这意味着该功能正在工作。然而,现在我得到了一个“未捕获的引用错误ajax未定义” 我试图摆弄index.js.erb,它抛出语法错误。以下是我的建议:删除
警报(“here2”)
并删除顶部的分号:“”+lead.attribute+“”
@Sunxperous:谢谢回答,请看我刚才添加的更新。您需要javascripts/application.js中的原型吗?@Sunxperous:Ummm。。不我正在使用jquery rails。我当时的印象是我不需要原型。我是不是遗漏了什么?对不起,我对web开发人员还很陌生。Prototype使用Ajax.Request,而jQuery使用。