小编典典

如何在RAILS中呈现AJAX响应

ajax

好的,很简单的问题,我正在使用一种AJAX方法,它的作用是通过查询在数据库中搜索特定信息。我可以在控制台中看到正在查询,甚至可以在Chrome开发者控制台中看到“发布”,当我单击它时,可以看到要显示的HTML,但是我不知道如何在页面中显示红外线。在此处输入图片说明

:3000 /已经有我要显示的html,但是我该如何更新呢?

我展示的方式是这样的…

<%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是我从查询保存项目的变量。

谢谢,我想我在这里很傻。对不起,我的英语不好。

更新:控制器看起来像这样:

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

阅读 229

收藏
2020-07-26

共1个答案

小编典典

在您的response_to块中,将format.js放在format.html上方。

确保控制器的视图文件夹中有index.js.erb。如果您使用的是Rails
3.1+,则index.js.erb(来自动作名称)应具有以下jQuery语句:

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

这将用ID DOMelementToPlaceContent用指定部分中的内容替换DOM元素的内容。

另外,您应该考虑将搜索逻辑移到同一控制器中的搜索操作,在这种情况下,您将需要在控制器的视图文件夹中使用search.js.erb文件。

2020-07-26