我在Rails 3应用程序中使用了Jvectormap。当我点击它时,我想隐藏地图,用选定国家的信息可视化一个部分。
我处理映射单击,然后向服务器发送一个GET req。
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 as / 参数:{“代码”“=>”ET“} 国家负荷(0.2ms)从“国家”中选择“Country”.* “国家”.“two_letter_code”= 'et‘限制1 呈现文本模板(0.0ms) 2毫秒内完成200 OK (视图: 0.6ms / ActiveRecord: 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>_statistics.html.erb
<%= link_to '<i class="icon-remove"></i>'.html_safe%>
<%=country%>
<%=@country%>现在,一切都正常,但部分并没有显示国家的价值。如果我必须在ajax get请求之后重新呈现部分,我不会取消,以及如何实现。
我试着用另一种方式改变控制器,但是行为是一样的。
def statistics
@country = Country.find_by_two_letter_code((params[:code]).downcase)
render 'users/_statistics'
end发布于 2013-10-15 05:29:15
我认为,由于您不需要刷新页面,只需要显示国家的详细信息,所以可以使用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>在控制器中
def statistics
@country = Country.find_by_two_letter_code((params[:code]).downcase)
respond_to do |format|
format.js
end
end在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部分必须有视图代码‘在’id 'place_partial_here‘,以便它可以用于进一步的ajax调用。
https://stackoverflow.com/questions/16884636
复制相似问题