1
使用acts-as-taggable-on和rails4-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)
是的。我試過這個答案,考慮到Rails 4中的差異,它仍然是相同的。 – 2015-03-30 20:40:57