如何呈现在Rails Ajax响应 [英] How to render the AJAX response in RAILS

查看:129
本文介绍了如何呈现在Rails Ajax响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,pretty的简单的问题,我有工作的AJAX方法,它的作用是,它在数据库中有一个查询特定信息的搜索。我可以在正在取得查询控制台看到的,我甚至可以看到邮报在Chrome开发者控制台,当我点击它,我可以看到我想要显示的HTML,但我有一个关于如何不艾芬想法为使IR在页面中。

中:3000 /已经有我想要显示的HTML,但我怎么更新?

我展示它的方式是这样的...

 <%如果不@ itemsok.nil?
  @ itemsok.each做|搜索|
%>
<表>
&其中; TR>
  < TD风格=宽度:100像素;><%= searches.first_item.description%> < / TD>
  < TD风格=宽度:150像素;字体大小:30像素;><%= searches.first_item_grade%> < / TD>


  < TD风格=宽度:150像素;><%= searches.second_item.description%> < / TD>
  < TD风格=宽度:150像素;字体大小:30像素;><%= searches.second_item_grade%> < / TD>
  < TD风格=宽度:150像素;字体大小:18像素;>< A HREF =<%= contribution_path(searches.id)%>>显示< / A> < / TD>


< / TR>
< /表>
 

@itemsok是我救的项目从查询的变量。

谢谢,我想我失去了一些东西很无聊的在这里。 对不起,我的可怕的英语。

更新:控制器看起来是这样的:

 高清指数
    尺寸1 = PARAMS [:尺寸1]
    号码1 = PARAMS [:号码1]
    号码2 = PARAMS [:数字2]
    quiero = PARAMS [:quiero]
    TENGO = PARAMS [:TENGO]

    如果(未number1.nilα)和(数字1!=)
      ITEM1 = Item.find(数量1)
    ELSIF不quiero.nil?
      quiero.strip!
      ITEM1 = Item.find(:第一,:条件=> ['?低(说明)LIKE',%#{quiero.downcase}%])
    结束

    如果(未number2.nilα)和(数字2!=)
      项目2 = Item.find(数字2)
    ELSIF不tengo.nil?
      tengo.strip!
      项目2 = Item.find(:第一,:条件=> ['?低(说明)LIKE',%#{tengo.downcase}%])
    结束

    如果(ITEM1和ITEM2)

      @itemsok = Contribution.where(first_item_id =?,item1.id)。凡(second_item_id =?,item2.id)。凡(second_item_grade =?,尺寸1)


      respond_to代码做|格式|
       的format.html
       format.js

      结束
    结束
 

解决方案

在你的respond_to块放format.js以上的fo​​rmat.html。

请确保你已经index.js.erb的在您的视图文件夹中的控制器。 这index.js.erb的(来自于动作名)应具有类似如下的jQuery的语句,如果您使用的是轨3.1 +:

  $('#DOMelementToPlaceContent)HTML(小于%= escape_javascript(渲染:部分=>中的部分/位置)%>);
 

这将取代ID DOMelementToPlaceContent的DOM元素的含量与内容指定的部分。

此外,你应该考虑移动逻辑在同一个控制器搜索行动在这种情况下,你需要在视图文件夹中的控制器search.js.erb文件搜索。

Ok, pretty simple question, I have working an AJAX method, what it does is that it searches in the database for specific information with a query. I can see in the Console that the query is being made, I can even see the "Post" in the Chrome Developer Console, when I click it I can see the HTML that I want to show, but i have no effing idea on how to render ir in the page.

the :3000/ already has the html I want to show, but how do I update it??

The way I am showing it is this...

<%if not @itemsok.nil? 
  @itemsok.each do |searches|
%>
<table>
<tr>
  <td style="width:100px;"><%= searches.first_item.description %>  </td>
  <td style="width:150px; font-size:30px;"><%= searches.first_item_grade %>  </td>


  <td style="width:150px;"><%= searches.second_item.description %> </td>
  <td style="width:150px; font-size:30px;"><%= searches.second_item_grade %>  </td>
  <td style="width:150px; font-size:18px;"><a href="<%= contribution_path(searches.id) %>">Show</a>  </td>


</tr>
</table>

@itemsok is the variable where I save the items from the query.

Thanks, I think I'm missing something very silly here. Sorry for my terrible english.

UPDATE: The controller looks like this:

def index
    size1 = params[:size1]
    number1 = params[:number1]
    number2 = params[:number2]
    quiero = params[:quiero]
    tengo = params[:tengo]

    if (not number1.nil?) and (number1 != "")
      item1 = Item.find(number1)
    elsif not quiero.nil?
      quiero.strip!
      item1 = Item.find(:first, :conditions => ['lower(description) LIKE ?', "%#{quiero.downcase}%"])
    end

    if (not number2.nil?) and (number2 != "")
      item2 = Item.find(number2)
    elsif not tengo.nil?
      tengo.strip!
      item2 = Item.find(:first, :conditions => ['lower(description) LIKE ?', "%#{tengo.downcase}%"])
    end

    if (item1 and item2)

      @itemsok = Contribution.where("first_item_id = ?",item1.id).where("second_item_id = ?",item2.id).where("second_item_grade = ?",size1)


      respond_to do |format|
       format.html
       format.js

      end
    end

解决方案

In your respond_to block put format.js above format.html.

Make sure you have index.js.erb in your view folder for the controller. This index.js.erb(comes from the action name) should have a jQuery statement like the following if you're using rails 3.1+:

$('#DOMelementToPlaceContent').html(<%= escape_javascript(render :partial => "partial/location") %>);

This will replace the content of the DOM element with ID DOMelementToPlaceContent with the content in the specified partial.

Also, you should think about moving the logic for your search to a search action in the same controller in which case you'll need a search.js.erb file in the view folder for the controller.

这篇关于如何呈现在Rails Ajax响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆