ajax获取请求后rails呈现部分 [英] rails render partial after ajax get request

查看:68
本文介绍了ajax获取请求后rails呈现部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我处理地图点击并向服务器发送 GET 请求.

users.js.coffee.erb

$ ->$("#world-map").vectorMaponRegionClick:(事件,代码)->$('#world-map').hide(1000)$.get('/users/statistics', {code: code});$('#statistics').show(1000)

users_controller.rb

def 统计@country = Country.find_by_two_letter_code((params[:code]).downcase)渲染:什么都没有=>真的

结束

这是回复

<块引用>

开始获取/users/statistics?code=ET";对于 127.0.0.1 在 2013-06-02 17:54:49 +0200

由 UsersController#statistics 处理为 /

参数:{code"=>ET"}

Country Load (0.2ms) SELECT "countries".* FROM "countries";哪里

"countries"."two_letter_code";= 'et' 限制 1

渲染文本模板(0.0ms)

在 2ms 内完成 200 OK(视图:0.6ms | ActiveRecord:0.2ms)

和视图

show.html.erb

 

<div id="世界地图";样式=宽度:620px;高度:300px"</div><div id="统计信息";样式=宽度:620px;高度:300px;显示:无;"><部分><%= render partial:'statistics', :locals =>{:国家=>@国家}%></节>

</p>

_statistics.html.erb

<%= link_to '<i class="icon-remove"></i>'.html_safe%><%=国家%><%=@country%>

现在,一切正常,但部分没有可视化国家/地区值.我不明白是否必须在 ajax get 请求后重新渲染部分,以及如何重新渲染.

我尝试以另一种方式更改控制器,但行为是相同的.

def 统计@country = Country.find_by_two_letter_code((params[:code]).downcase)呈现'用户/_统计'结尾

解决方案

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

show.html

<div id="world-map" style="width: 620px; height: 300px"></div><div id="statistics" style="width: 620px; height: 300px; display:none;"><部分><div id="place_partial_here"></div></节>

</p>

在控制器中

def 统计@country = Country.find_by_two_letter_code((params[:code]).downcase)response_to do |格式|格式.js结尾结尾

在 statistics.js.haml #我对 haml 很满意.

$('#place_partial_here').replaceWith("#{escape_javascript(render partial: 'country_data', locals: {country: @country})}");

在_country.html.haml

#place_partial_here #id 在haml 中使用hash 表示= country.name #显示国家名称= country.code #显示代码

部分必须在 id 'place_partial_here' 中包含视图代码,以便它可以在进一步的 ajax 调用中使用

I use Jvectormap into my Rails 3 app. When I click on it, I want to hide the map and visualize a partial with information about the selected country.

I handle the map click and I send a GET req to the server.

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

end

this is the response

Started GET "/users/statistics?code=ET" for 127.0.0.1 at 2013-06-02 17:54:49 +0200

Processing by UsersController#statistics as /

Parameters: {"code"=>"ET"}

Country Load (0.2ms) SELECT "countries".* FROM "countries" WHERE

"countries"."two_letter_code" = 'et' LIMIT 1

Rendered text template (0.0ms)

Completed 200 OK in 2ms (Views: 0.6ms | ActiveRecord: 0.2ms)

and the view

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%>

Now, everything works, but the partial doesn't visualize the country value. I don't undestand if I have to re-render the partial after the ajax get request, and how.

I've tried to change the controller in another way, but the behavior is the same.

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

解决方案

i think that since you don't need a page refresh and just need to display the details the country, you can use 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>

in the controller

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

in statistics.js.haml #i am comfortable with haml.

$('#place_partial_here').replaceWith("#{escape_javascript(render partial: 'country_data', locals: {country: @country})}");

in the _country.html.haml

#place_partial_here #id is denoted in haml using hash
  = country.name #display country name
  = country.code #display code

the partial must have the view code 'inside' the id 'place_partial_here' so that it can be used in further ajax calls

这篇关于ajax获取请求后rails呈现部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆