rails json查询关于habtm的分页

时间:2017-01-18 17:57:47

标签: ruby-on-rails json

我有两个模型:<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script><link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <body> <button class = "btn btn-file btn-md q_info" data-toggle="modal" data-target="#myModal">click</button> <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 id="modal-title"> </h4> </div> <div class="modal-body"> <div class="panel-group" id="accordion"> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse1">Course Introduction </a> </h4> </div> <div id="collapse1" class="panel-collapse collapse in"> <div class="panel-body"> <p>abc</p> </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse2">Description </a> </h4> </div> <div id="collapse2" class="panel-collapse collapse"> <div class="panel-body"> <p>Lorem </p> </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse3">Goals </a> </h4> </div> <div id="collapse3" class="panel-collapse collapse"> <div class="panel-body"> <p>Lorem ipsum dolor sit </p> </div> </div> </div> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div></div> <!-- end of Modal --> </body>Document,它们相互之间是相互关联的。

在搜索表单中,我想实现一个自动完成功能,该功能提出与文档相关的位置(即不是Place表中的所有位置)。

这是我的代码:

places

这是服务器日志显示的内容:

def enum_places
    @locus = Place.joins(:documents).map{ |f| f.place }.where("place like ?", "%#{params[:term]}%")
    render json @locus.map(&:place)
end

当然,错误是:

Processing by DocumentsController#show as JSON
Parameters: {"term"=>"Dou", "id"=>"enumplaces"}
Acte Load (1.2ms)  SELECT  "documents".* FROM "documents" WHERE "documents"."id" = $1 LIMIT $2  [["id", 0], ["LIMIT", 1]]

事实证明,自动填充功能正在寻找的网址是

RecordNotFound (Couldn't find Document with 'id'=enumplace):

这是在我的routes.rb:

http://localhost:3000/documents/enumplace?term=Dou

当我做铁路线时,这条线似乎是正确的:

get 'documents/enumplace' => 'documents#enum_place'

但....在documents_controller.rb中调用的方法是...... show!

谢谢!

2 个答案:

答案 0 :(得分:0)

在路线中尝试:

resources :documents do 
  get :autocomplete_document_place, :on => :collection
end

DocumentsController
autocomplete :document, :place

答案 1 :(得分:0)

知道了!

  1. 我需要为&#34; show&#34;。
  2. 添加正确的路线
  3. 搜索地点的代码是

    def enum_document_place      @locus = Place.joins(:documents).where(&#34;地方喜欢?&#34;,&#34;%#{params [:term]}%&#34;)      渲染json:@ locus.map(&amp;:place)  端