Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 魔术建议返回后404错误_Ruby On Rails 4_Autocomplete_Ruby On Rails 3.2_Magicsuggest - Fatal编程技术网

Ruby on rails 4 魔术建议返回后404错误

Ruby on rails 4 魔术建议返回后404错误,ruby-on-rails-4,autocomplete,ruby-on-rails-3.2,magicsuggest,Ruby On Rails 4,Autocomplete,Ruby On Rails 3.2,Magicsuggest,我正在尝试使用Magic Suggest让autocomplete在我的rails应用程序中工作 我认为这是一个正确的问题:如何让MagicSuggest获取我给出的URL上的JSON 这是console在我键入字母时返回的错误: POST http://localhost:3000/search_foods 404 (Not Found) jquery.js:8706 Uncaught Could not reach server 以下是神奇的建议代码: input.magicSugge

我正在尝试使用Magic Suggest让autocomplete在我的rails应用程序中工作

我认为这是一个正确的问题:如何让MagicSuggest获取我给出的URL上的JSON

这是console在我键入字母时返回的错误:

POST http://localhost:3000/search_foods 404 (Not Found) jquery.js:8706
Uncaught Could not reach server 
以下是神奇的建议代码:

 input.magicSuggest({
         data: "/foods/search/",
         placeholder: "Search Foods...",
         valueField:'idFood',
         displayField:'foodName'
       });    
路线

resources :search_foods   
控制器和动作

class SearchFoodsController < ApplicationController
  def index
    render json: %['Crack', 'Cocain', 'Gorilla Test', 'Horse Test']
  end
end
正如我的代码设计的那样

我认为问题在于MagicSuggest默认发送POST请求,尽管我不确定这是否完全相关:

You can pass the url from which the component will fetch its JSON data.Data will be fetched
         *     using a POST ajax request that will * include the entered text as 'query' parameter. The results
         *     fetched from the server can be:
         *     - an array of JSON objects (ex: [{id:...,name:...},{...}])
         *     - a string containing an array of JSON objects ready to be parsed (ex: "[{id:...,name:...},{...}]")
         *     - a JSON object whose data will be contained in the results property
         *      (ex: {results: [{id:...,name:...},{...}]
试试这个:

input.magicSuggest({
         data: "http://localhost:3000/search_foods",
         placeholder: "Search Foods...",
         valueField:'idFood',
         displayField:'foodName'
});

该文件规定,该组件应满足以下条件之一:

     *     - an array of JSON objects (ex: [{id:...,name:...},{...}])
     *     - a string containing an array of JSON objects ready to be parsed (ex: "[{id:...,name:...},{...}]")
     *     - a JSON object whose data will be contained in the results property
     *      (ex: {results: [{id:...,name:...},{...}]
当您访问
/search\u foods
时,您会得到

'Crack', 'Cocain', 'Gorilla Test', 'Horse Test'

这不符合支持的3种情况中的任何一种。

我对POST请求的怀疑是正确的

我的朋友帮了我,所以我才能够解决这个问题

我就是这么做的

  • 取消了FoodSearch控制器,因为根本不需要它

  • 在我的食物控制器中创建了搜索操作,如下所示:

    def搜索 呈现json:['cocain','crack','gorilla-testone'] 结束

  • 将我的路线编辑为POST请求而不是get*这是关键:

    资源:有食物吗 收藏办 帖子:搜索 结束 结束

  • ---正如karlipoppins所建议的,另一种选择是简单地更改magicSuggest发出的请求类型,方法属性如下:

            input.magicSuggest({
               method: 'get',
               data: "/foods/search/",
               placeholder: "Search Foods...",
               valueField:'idFood',
               displayField:'foodName'
            }); 
    
    那我就不需要改变去邮局的路线了

  • 将此路径添加到js中的数据属性

    数据:“/foods/search/”


  • 这对任何试图让magicSuggest在rails中工作的人来说都是一个巨大的帮助。老实说,这是一个非常简单的设置。这一点和JSON格式是唯一绊倒我的地方

    它的意义是什么?哈哈,不然我就不知道了。我想这是我机器上的默认设置。当你说你直接访问url时,你是在暗示你在浏览器中键入内容并得到结果吗?是的,当我键入准确的url时,我得到了我期望的结果。嗯。。这是结果的源代码视图。”破解“,”Cocain“,”Gorilla Test“,”Horse Test“POST http://localhost:3000/search_foods 404(未找到)没有空格(删除http://part也是如此)请检查您的服务器/控制器代码是否有映射来处理到http://localhost:3000/search_foods的POST调用。此外,您的响应不是magicsugest.Ok所要求的JSON格式。请给我一点时间弄清楚怎么做。我很高兴你发现了你的问题,但这与magicsuggest无关。因此,这只是从rails控制器输出json的标准方式,在您执行任何ajax操作时都会使用。Magicsuggest不关心格式是如何实现的,只要格式正确。如果希望组件执行get请求而不是post请求,则可以在创建组件时将属性
    method
    设置为
    get
    。啊。。我明白了。我正在寻找一种方法,将magicSuggest提出的请求类型更改为get请求。您刚才指出我需要查找“method”属性。。我不知道为什么我没有找一个有这个名字的房子。无论如何,谢谢你。
            input.magicSuggest({
               method: 'get',
               data: "/foods/search/",
               placeholder: "Search Foods...",
               valueField:'idFood',
               displayField:'foodName'
            });