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