1

使用acts-as-taggable-onrails4-autocomplete自动完成标签。这是我的代码。Rails:无法找到发布'id'= autocomplete_tag_name

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)未找到作为

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

火狐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) 

它找不到一个ID为autocomplete_tag_name的帖子,它不应该是这样的。

如果我硬编码请求

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

它返回200(OK)

回答

1

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

+0

是的。我试过这个答案,考虑到Rails 4中的差异,它仍然是相同的。 – 2015-03-30 20:40:57