更新内容与的link_to:远程=>真

问题描述:

我目前正试图与我通过特定的控制器的动作发回的内容页面上更新特定<DIV>元素。我目前实现这样的:更新内容与的link_to:远程=>真

查看:

link_to "Country: #{@vacancy.country.name}", semantic_country_url(@vacancy.country.id), :remote => true, :update => :semantic 

控制器:

class SemanticController < ApplicationController 
    # This action will communicate with DBPedia to retrieve information using the SPARQL endpoint 
    # about the name of the specified city. 
    def country 
    country = Country.find params[:id] 
    engine = SemanticSearchEngine.new 
    @country = engine.country_information country.name, 'en' 
    render(:update) { |page| page.replace_html 'semantic', :partial => 'semantic/country', :layout => false} 
    end 
end 

这确实控制器的魔术和在ID “语义” 更新<DIV>包含链接的页面。然而,我不想在我的控制器中有这个javascript逻辑,但我想在一个单独的jrs文件中有这个(我使用HAML,所以它可能是a.haml.jrs文件)。

但我不明白如何我应该做这个。我试图创建一个包含JavaScript的执行更新一个JRS文件,但是当我打电话的动作,我可以在Firebug看到JavaScript的beeing返回,但什么也没有更新。

有人能向我解释,我怎样才能使它发挥作用?

编辑 我已经添加了country.js.erb文件,其中包含下面的代码的应用程序:

$("semantic").replace(); 
$("semantic").insert("<p>#{@country.abstract.value}</p><p>#{@country.comment.value}</p><p>Currency: #{@country.currency.value}</p><p>Population: #{@country.population.value}</p><p>Capital: #{@country.capital.value}</p>"); 

在控制器我已经放在渲染线注释让轨道选择基于请求的正确呈现模板。这应该是country.js.haml文件通常。当在服务器开发日志看,我看到以下条目的链接,当点击:

入门使用“/语义/国家/ 5”为127.0.0.1,在周三8月24日9时05分52秒 + 0200 2011 SemanticController处理#country as JS
参数:{“id”=>“5”}国家负载(2.0ms)SELECT countries。* FROM countries WHERE countriesid = 5 LIMIT 1渲染在944ms(查看: 70.0ms |的ActiveRecord:2.0ms)完成200 OK 语义/ country.js.haml(4.0ms)

因此,我们可以得出这样的结论远程链路是按预期工作。返回的Javascript是这样的:

$("semantic").replace(); 
$("semantic").insert("<p>#{@country.abstract.value}</p><p>#{@country.comment.value}</p><p>Currency: #{@country.currency.value}</p><p>Population: #{@country.population.value}</p><p>Capital: #{@country.capital.value}</p>"); 

我不认为这是它应该是什么?

EDIT2 重命名的文件到country.js。该局并插入下面的代码:

$('semantic').replace(); 
$('semantic').insert('<p><%= @country.abstract.value %></p><p><%= @country.comment.value%></p><p>Currency: <%@country.currency.value%></p><p>Population: <%[email protected]%></p><p>Capital: <%[email protected]%></p>'); 

来自服务器的响应是HTTP 200与下列内容:

$('semantic').replace(); 
$('semantic').insert('<p>The United Kingdom of Great Britain and Northern Ireland (commonly known as the United Kingdom, the UK, or Britain) is a country and sovereign state located off the northwestern coast of continental Europe. It is an island nation, spanning an archipelago including Great Britain, the northeastern part of the island of Ireland, and many smaller islands. Northern Ireland is the only part of the UK with a land border with another sovereign state, sharing it with the Republic of Ireland. Apart from this land border, the UK is surrounded by the Atlantic Ocean, the North Sea, the English Channel and the Irish Sea. Great Britain is linked to continental Europe by the Channel Tunnel. The United Kingdom is a constitutional monarchy and unitary state consisting of four countries: England, Northern Ireland, Scotland and Wales. It is governed by a parliamentary system with its seat of government in London, the capital, but with three devolved national administrations of varying powers in Belfast, Cardiff and Edinburgh, the capitals of Northern Ireland, Wales and Scotland respectively. The Channel Island bailiwicks of Jersey and Guernsey, and the Isle of Man are Crown Dependencies, which means they are constitutionally tied to the British monarch but are not part of the UK. The UK has fourteen overseas territories that are not constitutionally part of the UK. These territories are remnants of the British Empire, which at its height in 1922 encompassed almost a quarter of the world's land surface, the largest empire in history. British influence can still be observed in the language, culture and legal systems of many of its former colonies. The UK is a developed country, with the world's sixth largest economy by nominal GDP and the sixth largest by purchasing power parity. It was the world's first industrialised country and the world's foremost power during the 19th and early 20th centuries, but the economic and social cost of two world wars and the decline of its empire in the latter half of the 20th century diminished its leading role in global affairs. The UK nevertheless remains a great power with leading economic, cultural, military, scientific and political influence. It is a recognised nuclear weapons state while its military expenditure ranks third or fourth in the world, depending on the method of calculation. It is a Member State of the European Union, a permanent member of the United Nations Security Council, a member of the Commonwealth of Nations, G8, G20, NATO, OECD and the World Trade Organization.</p><p>The United Kingdom of Great Britain and Northern Ireland (commonly known as the United Kingdom, the UK, or Britain) is a country and sovereign state located off the northwestern coast of continental Europe. It is an island nation, spanning an archipelago including Great Britain, the northeastern part of the island of Ireland, and many smaller islands. Northern Ireland is the only part of the UK with a land border with another sovereign state, sharing it with the Republic of Ireland.</p><p>Currency: </p><p>Population: 62041708</p><p>Capital: London</p>'); 

但内容依然没有beeing插入...

编辑3 我的一位同事说,对代码没有约束力。 (例如'oops')会被触发,但是当我们尝试选择元素并使用$('name')时,它会返回null,即使我们知道这些东西是definitly并且可以在firebug控制台中工作。所以似乎有一些缺失,但我不知道是什么。

EDIT 4 创建了一个包含以下代码的新的JavaScript文件:

document.observe("dom:loaded", function(){ 
$('#semantic') 
    .bind("ajax:success", function(evt, data, status, xhr){ 
     // Since we're dealing with ajax call, we'll handle the response as JavaScript 
     eval(xhr.responseText); 
    }) 
    .bind("ajax:error", function(evt, data, status, xhr){ 
     // Insert a custom error message when something goes wrong 
     $('#semantic').replace(); 
     $('#semantic').insert('<p>A problem occured retrieving the information.</p>'); 
    }); 
}); 

显然根据http://www.alfajango.com/blog/rails-3-remote-links-and-forms/我需要绑定自己的回调,我已经与上面的脚本来完成,但仍然没有骰子....

+0

我对原型不熟悉,但是您是否尝试在'country.js.erb'文件中简单地放置'page.replace_html'语义':partial =>'语义/国家'? – apneadiving

+0

你可以在你的问题中包含正在返回的JavaScript。 – mark

+0

我将代码再次更新到jrs文件,给我一些分钟重新运行应用程序 –

终于解决了这个残局:

创建JavaScript文件绑定回调

document.observe("dom:loaded", function(){ 
    $('semantic') 
     .observe("ajax:success", function(evt, data, status, xhr){ 
      alert("success"); 
      // Since we're dealing with ajax call, we'll handle the response as JavaScript 
      eval(xhr.responseText); 
     }) 
     .observe("ajax:error", function(evt, data, status, xhr){ 
      alert("failed"); 
      // Insert a custom error message when something goes wrong 
      $('semantic').replace(); 
      $('semantic').insert('<p>A problem occured retrieving the information.</p>'); 
     }); 
}); 

依赖的Prototype.js文件在内,大多数网络的例子使用jquery,所以不得不以适应多个呼叫以符合原型标准。

创建js.erb文件

$('semantic').innerHTML = ""; 
$('semantic').insert("<p><%= CGI::escape(@country.abstract.value) %></p><p><%= CGI::escape(@country.comment.value)%></p><p>Currency: <%= CGI::escape(@country.currency.value) %></p><p>Population: <%= CGI::escape(@country.population.value) %></p><p>Capital: <%= CGI::escape(@country.capital.value) %></p>"); 

这里的问题是$( '语义')。REPLACE(),这是由于某种原因,删除元素,而不是其清除。

通过这些更改,远程呼叫按预期工作。

当你告诉它时,你的国家动作会自动渲染一个country.js。[格式]文件。只要把JS/JS代码生成到该文件,做这样的事情在你的控制器底部:

+0

我知道,问题在于Javascript没有做任何事情 –

+0

这可能是因为你'再说你的js文件是一个haml文件。但是'$(“语义”).....'在haml中有效吗? – pduersteler

+0

该文件被命名为country.js.erb,它只是返回原始文章中的内容。 我需要添加页面

不宜$("semantic")$("#semantic")

+0

修改它,因为你说,没有任何区别。刚刚得到生成的JavaScript回来,但它不beeing执行 –

+0

@NekoNova,'$(“#语义”)。html(........)'? – YOU

+0

什么都没有。我不认为javascript语法是问题,请参阅Prototype API。呼叫完成后问题就出现了。 –