当我在表单的主题字段中键入现有主题时收到此错误:
Started GET "/posts/autocomplete_topic_name?term=test" for 127.0.0.1 at 2011-05-21 10:51:19 -0700
Processing by PostsController#show as JSON
Parameters: {"term"=>"test", "id"=>"autocomplete_topic_name"}
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = 0 LIMIT 1
Completed in 28ms
ActiveRecord::RecordNotFound (Couldn't find Post with ID=autocomplete_topic_name):
app/controllers/posts_controller.rb:9:in `show'
Rendered /home/dude/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.5ms)
Rendered /home/dude/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (4.2ms)
Rendered /home/dude/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (14.2ms)
主题为“测试”和“测试”。
模型:
class Topic < ActiveRecord::Base
has_many :posts
validates :name, :presence => true,
attr_accessible :name, :post_id
end
class Post < ActiveRecord::Base
belongs_to :topic
accepts_nested_attributes_for :topic
attr_accessible :name, :title, :content, :topic, :topic_attributes
end
视图:
/ posts / new仅呈现此形式:(使用simple_form)
<%= simple_form_for :post, :url => { :controller => :posts, :action => "create" } do |f| %>
<h1>Create a Post</h1>
<%= f.input :name, :label => false, :placeholder => "Name (optional)", :required => false %>
<%= f.input :title, :label => false, :placeholder => "Title" %>
<%= f.input :content, :label => false, :placeholder => "Content", :as => :text %>
<%= f.simple_fields_for :topic do |topic_form| %>
<%= topic_form.input :name, :label => false, :placeholder => "Topic", :url => autocomplete_topic_name_path, :as => :autocomplete %>
<% end %>
<%= raw recaptcha_tags -%>
<%= f.button :submit, "Post" %>
<% end %>
控制器:
class PostsController < ApplicationController
autocomplete :topic, :name, :full => true
respond_to :html, :js, :xml
def new
@post = Post.new
end
def create
topic_name = params[:post].delete(:topic)
@topic = Topic.find_or_create_by_name(topic_name)
@post = Post.new(params[:post])
@post.topic = @topic
respond_to do |format|
if verify_recaptcha(:model => @post) && @post.save
format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
else
flash.delete(:recaptcha_error)
flash.now[:error] = "Incorrect word verification. Are you sure you\'re human?"
format.html { render :action => "new" }
end
end
end
end
Posts控制器的第9行是:
@post = Post.find(params[:id])
在帖子#show
中routes.rb中:
MyApp::Application.routes.draw do
root :to => "posts#index"
resources :topics
resources :posts
resources :comments
match "posts/autocomplete_topic_name", :as => "autocomplete_topic_name"
end
这是我的宝石文件:
gem 'rails', '3.0.6'
gem 'sqlite3'
gem 'ancestry'
gem 'jquery-rails', '>= 1.0.3'
gem 'kaminari'
gem 'nifty-generators'
gem 'rdiscount'
gem 'rails3-jquery-autocomplete'
gem 'recaptcha', :require => 'recaptcha/rails'
gem 'simple_form'
和架构:
create_table "posts", :force => true do |t|
t.string "name"
t.string "title"
t.text "content"
t.integer "topic_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "topics", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
谢谢你看看。
答案 0 :(得分:1)
在您的路线文件中,尝试在“资源:帖子”之前移动“匹配'帖子/ ...'”行。