Rails:无法#39;使用'' = autocomplete tag_name查找帖子

时间:2015-03-30 20:14:28

标签: ruby-on-rails autocomplete jquery-autocomplete acts-as-taggable-on

使用acts-as-taggable-onrails4-autocomplete自动填充代码。这是我的代码。

的routes.rb

  get 'tags/:tag', to: 'posts#index', as: :tag

  resources :posts do
    get :autocomplete_tag_name, :on => :collection
  end

posts_controller.rb

autocomplete :tag, :name

的application.js

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require autocomplete-rails

_form.html.haml

= f.input :tag_list, :url => autocomplete_tag_name_posts_path, :as => :autocomplete

当我开始输入时,我可以看到对服务器的请求,但返回(404)Not Found as

http://localhost:3000/posts/autocomplete_tag_name?term=rails

Firefox DevTool网络

ActiveRecord::RecordNotFound at /posts/autocomplete_tag_name
============================================================

> Couldn't find Post with 'id'=autocomplete_tag_name

app/controllers/posts_controller.rb, line 133
---------------------------------------------

``` ruby
  128     end
  129   
  130     private
  131       # Use callbacks to share common setup or constraints between actions.
  132       def set_post
> 133         @post = Post.find(params[:id])
  134       end
  135   
  136       # Never trust parameters from the scary internet, only allow the white list through.
  137       def post_params
  138         params.require(:post).permit(:title, :body, :image, :tag_list)

找不到标识为autocomplete_tag_name的帖子,它不应该是这样的。

如果我将请求硬编码为

http://localhost:3000/tags/autocomplete_tag_name?term=rails

它返回200(ok)

1 个答案:

答案 0 :(得分:1)

我认为这是路由错误。查看硬编码链接,它位于localhost:3000/tags/,但您的404错误正在寻找localhost:3000/posts/。看看这篇文章的答案,它的Rails3,但它可能有一些用处:How to add tagging with autocomplete to an existing model in Rails?