首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails在ajax请求后呈现部分

rails在ajax请求后呈现部分
EN

Stack Overflow用户
提问于 2013-06-02 16:10:17
回答 1查看 3.6K关注 0票数 0

我在Rails 3应用程序中使用了Jvectormap。当我点击它时,我想隐藏地图,用选定国家的信息可视化一个部分。

我处理映射单击,然后向服务器发送一个GET req。

users.js.coffee.erb

代码语言:javascript
复制
$ ->
  $("#world-map").vectorMap
    onRegionClick: (event, code) ->
      $('#world-map').hide(1000)
      $.get('/users/statistics', {code: code});
      $('#statistics').show(1000)

users_controller.rb

代码语言:javascript
复制
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

代码语言:javascript
复制
 <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

代码语言:javascript
复制
<%= link_to '<i class="icon-remove"></i>'.html_safe%>
<%=country%>
<%=@country%>

现在,一切都正常,但部分并没有显示国家的价值。如果我必须在ajax get请求之后重新呈现部分,我不会取消,以及如何实现。

我试着用另一种方式改变控制器,但是行为是一样的。

代码语言:javascript
复制
def statistics
 @country = Country.find_by_two_letter_code((params[:code]).downcase)  
 render 'users/_statistics'
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-15 05:29:15

我认为,由于您不需要刷新页面,只需要显示国家的详细信息,所以可以使用ajax。

show.html

代码语言:javascript
复制
<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>

在控制器中

代码语言:javascript
复制
def statistics
  @country = Country.find_by_two_letter_code((params[:code]).downcase)  
  respond_to do |format|
    format.js
  end
end

在statistics.js.haml #,我和haml在一起很舒服。

代码语言:javascript
复制
$('#place_partial_here').replaceWith("#{escape_javascript(render partial: 'country_data', locals: {country: @country})}");

在_country.html.haml中

代码语言:javascript
复制
#place_partial_here #id is denoted in haml using hash
  = country.name #display country name
  = country.code #display code

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16884636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档