在ajax获取请求后,rails渲染部分

我在我的Rails 3应用程序中使用Jvectormap。 当我点击它时,我想隐藏地图并使用有关所选国家/地区的信息可视化部分。

我处理地图点击,然后向服务器发送GET请求。

users.js.coffee.erb

$ -> $("#world-map").vectorMap onRegionClick: (event, code) -> $('#world-map').hide(1000) $.get('/users/statistics', {code: code}); $('#statistics').show(1000) 

users_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的GET“/ users / statistics?code = ET”

由UsersController处理#statistics为/

参数:{“code”=>“ET”}

国家负荷(0.2ms)选择“国家”。* FROM“国家”地点

“countries”。“two_letter_code”=’et’LIMIT 1

渲染文本模板(0.0ms)

2ms完成200 OK(浏览次数:0.6ms | ActiveRecord:0.2ms)

和观点

show.html.erb

  

{:country => @country} %>

_statistics.html.erb

 <%= link_to ''.html_safe%>   

现在,一切正常,但部分不会显示国家价值。 如果我必须在ajax get请求之后重新渲染部分,我不知道如何。

我试图以另一种方式更改控制器,但行为是一样的。

 def statistics @country = Country.find_by_two_letter_code((params[:code]).downcase) render 'users/_statistics' end 

我认为,既然你不需要页面刷新,只需要显示国家的详细信息,你可以使用ajax。

show.html

 

在控制器中

 def statistics @country = Country.find_by_two_letter_code((params[:code]).downcase) respond_to do |format| format.js end end 

在statistics.js.haml #i我很熟悉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 

部分必须在id’place_partial_here’中具有视图代码’inside’,以便它可以用于进一步的ajax调用

你试过这个吗?

render:partial =>“statistics”,:collection => @country