[英]Rails: Couldn't find Post with 'id'=autocomplete_tag_name
Using acts-as-taggable-on and rails4-autocomplete to autocomplete tags. 使用行为可标记和rails4-autocomplete来自动完成标记。 Here is my code. 这是我的代码。
routes.rb 的routes.rb
get 'tags/:tag', to: 'posts#index', as: :tag
resources :posts do
get :autocomplete_tag_name, :on => :collection
end
posts_controller.rb posts_controller.rb
autocomplete :tag, :name
application.js 的application.js
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require autocomplete-rails
_form.html.haml _form.html.haml
= f.input :tag_list, :url => autocomplete_tag_name_posts_path, :as => :autocomplete
When I start typing, I can see the request to the server but return (404) Not Found as 当我开始输入内容时,我可以看到对服务器的请求,但是返回(404)Not Found as
http://localhost:3000/posts/autocomplete_tag_name?term=rails
Firefox DevTool Network 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)
It couldn't find a post with id autocomplete_tag_name
which it shouldn't be like this. 它找不到ID为autocomplete_tag_name
的帖子,它不应该这样。
If I hard coded the request to 如果我将请求硬编码为
http://localhost:3000/tags/autocomplete_tag_name?term=rails
It returns 200 (ok) 返回200(确定)
I think this is a routing error. 我认为这是一个路由错误。 Looking at the hard coded link, it is working at localhost:3000/tags/
, but your 404 error is looking for localhost:3000/posts/
. 查看硬编码链接,它在localhost:3000/tags/
,但是您的404错误正在寻找localhost:3000/posts/
。 Have a look at the answer on this post, its for Rails3 but it may be of some use: How to add tagging with autocomplete to an existing model in Rails? 看一下这篇文章的答案,它适用于Rails3,但它可能有一些用处: 如何将具有自动完成功能的标记添加到Rails中的现有模型?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.