views:

102

answers:

1

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 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 }
A: 
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"
Salil
Tried the routes.rb option and can't seem to get it to work. With respect to your second options, where exactly do I put that? In the <%= text_field_with_auto_complete :tag, :tag_name, {}, {:method => :get} %> code? What is the syntax?
bgadoci
Ah, got it. Had to also ad the auto_complete_for :tag, :tag_name in the posts controller after modifying routs. Thanks so much.
bgadoci