Ruby on rails rails在ajaxget请求后呈现部分
我在Rails 3应用程序中使用Jvectormap。 当我点击它时,我想隐藏地图并可视化包含所选国家信息的部分地图 我处理映射单击并向服务器发送GET req users.js.coffee.erbRuby on rails rails在ajaxget请求后呈现部分,ruby-on-rails,ajax,render,partial,Ruby On Rails,Ajax,Render,Partial,我在Rails 3应用程序中使用Jvectormap。 当我点击它时,我想隐藏地图并可视化包含所选国家信息的部分地图 我处理映射单击并向服务器发送GET req users.js.coffee.erb $ -> $("#world-map").vectorMap onRegionClick: (event, code) -> $('#world-map').hide(1000) $.get('/users/statistics'
$ ->
$("#world-map").vectorMap
onRegionClick: (event, code) ->
$('#world-map').hide(1000)
$.get('/users/statistics', {code: code});
$('#statistics').show(1000)
用户\u controller.rb
def statistics
@country = Country.find_by_two_letter_code((params[:code]).downcase)
render :nothing => true
结束
这就是答案
在2013-06-02 17:54:49+0200时开始获取/用户/统计数据?代码=127.0.0.1的ET
由UsersControllerstatistics处理为/
参数:{code=>ET}
国家/地区负载0.2ms选择国家/地区。*来自
countries.two_字母代码='et'限值1
渲染文本模板0.0ms
在2ms视图中完成200 OK:0.6ms |活动记录:0.2ms
那景色呢
show.html.erb
<p> <div id="world-map" style="width: 620px; height: 300px"></div>
<div id="statistics" style="width: 620px; height: 300px; display:none;">
<section>
<%= render partial:'statistics', :locals => {:country => @country} %>
</section>
</div>
</p>
你试过这个吗
render:partial=>statistics,:collection=>@country我认为,因为您不需要刷新页面,只需要显示国家的详细信息,所以可以使用ajax show.html
<p> <div id="world-map" style="width: 620px; height: 300px"></div>
<div id="statistics" style="width: 620px; height: 300px; display:none;">
<section>
<div id="place_partial_here"></div>
</section>
</div>
</p>
在statistics.js.haml中,我对haml很满意
$('#place_partial_here').replaceWith("#{escape_javascript(render partial: 'country_data', locals: {country: @country})}");
在_country.html.haml
#place_partial_here #id is denoted in haml using hash
= country.name #display country name
= country.code #display code
partial必须在id“place_partial_here”中包含视图代码“inside”,以便在进一步的ajax调用中使用它尝试使用respond_whit@country而不是render xxxx?请在没有u、ur等的情况下编写正确的英语,好吗?
def statistics
@country = Country.find_by_two_letter_code((params[:code]).downcase)
respond_to do |format|
format.js
end
end
$('#place_partial_here').replaceWith("#{escape_javascript(render partial: 'country_data', locals: {country: @country})}");
#place_partial_here #id is denoted in haml using hash
= country.name #display country name
= country.code #display code