开发者

Autocomplete Error Question - Ruby on Rails

开发者 https://www.devze.com 2022-12-26 14:11 出处:网络
I have built a very simple blog application using Ruby on Rails. New to both Ruby and Rails so excuse the stupid questions.

I have built a very simple blog application using Ruby on Rails. New to both Ruby and Rails so excuse the stupid questions.

I currently have two tables that relate to this question. I have a Post table and a Tag table. Basically I set it up such that Post has_many :tags and Tag belongs_to :post. I am using AJAX开发者_如何学C to process and display the tags in the show view of the post.

I installed the auto_complete plugin and I am getting an error when I enter the letters in the text_field_with_auto_complete for tag creation. My suspicion is this is because the form is a remote_form_for or something I am doing wrong in the routes.rb. Here is the error and code:

Error

Processing PostsController#show (for 127.0.0.1 at 2010-04-13 23:25:46) [GET]
  Parameters: {"tag"=>{"tag_name"=>"f"}, "id"=>"auto_complete_for_tag_tag_name"}
  Post Load (0.1ms)   SELECT * FROM "posts" WHERE ("posts"."id" = 0) 

ActiveRecord::RecordNotFound (Couldn't find Post with ID=auto_complete_for_tag_tag_name):
  app/controllers/posts_controller.rb:22:in `show'

Rendered rescues/_trace (26.0ms)
Rendered rescues/_request_and_response (0.2ms)
Rendering rescues/layout (not_found)

remote_form_for located in /views/posts/show.html.erb

<% remote_form_for [@post, Tag.new] do |f| %>
    <p>
        <%= f.label :tag_name, "Tag" %><br/>
        <%= text_field_with_auto_complete :tag, :tag_name, {}, {:method => :get} %>
    </p>
    <p><%= f.submit "Add Comment" %></p>
<% end %>

tags_controller.rb (I'll spare you all the actions but added the following here)

auto_complete_for :tag, :tag_name

routes.rb

  map.resources :posts, :has_many => :comments
  map.resources :posts, :has_many => :tags
  map.resources :tags, :collection => {:auto_complete_for_tag_tag_name => :get }


ActiveRecord::RecordNotFound (Couldn't find Post with ID=auto_complete_for_tag_tag_name):
  app/controllers/posts_controller.rb:22:in `show'

This shows that rails searching "auto_complete_for_tag_tag_name" method in posts controller so either modify routes.rb

map.resources :posts, :collection => {:auto_complete_for_tag_tag_name => :get }

OR

Change remote form so that it should call method   
"/tags/auto_complete_for_tag_tag_name"
    instead of  
"/posts/auto_complete_for_tag_tag_name"
0

精彩评论

暂无评论...
验证码 换一张
取 消