将PgSearch添加到Rails app:`ActiveRecord :: RecordNotFound`

时间:2014-10-17 21:47:13

标签: ruby-on-rails postgresql pg-search

我试图将Paperclip添加到Forem-based Rails app

直播应用:http://runnable.com/VEFe5hZXSE9z0q5x/forem-with-pgsearch-for-ruby-on-rails(在点击/etc/init.d/postgresql start按钮之前运行Run

但是,为什么我会在ActiveRecord::RecordNotFound找到一个存在的帖子后获得app/decorators/models/forem/topic_decorator.rb

Forem::Topic.class_eval do include PgSearch pg_search_scope :search, against: [:subject], using: { tsearch: { dictionary: "english" } }, associated_against: { posts: [:text] } end

app/decorators/controllers/forem/forums_controller_decorator.rb

Forem::ForumsController.class_eval do def search @search = params[:keywords] @topics = Forem::Topic.search(@search) end end

app/views/layouts/application.html.erb

<div class="search"> <%= form_tag main_app.search_path, method: :get do %> <label for="search">Search</label> <%= search_field_tag :keywords, params[:keywords], id: "search" %> <% end %> </div>

app/views/forem/forums/search.html.erb

<h1>Search results</h1> <%= render @topics %>

config/routes.rb

get '/search', to: 'forem/forums#search', as: 'search'

Mangos

搜索Started GET "/search?utf8=%E2%9C%93&keywords=Mangos" at 2014-10-17 19:33:15 +0000 Processing by Forem::ForumsController#show as HTML Parameters: {"utf8"=>"?", "keywords"=>"Mangos", "id"=>"search"} [1m[35mForem::Forum Load (0.5ms)[0m SELECT "forem_forums".* FROM "forem_forums" WHERE "forem_forums"."slug" = 'search' ORDER BY "forem_forums"."name" ASC LIMIT 1 Completed 404 Not Found in 7ms ActiveRecord::RecordNotFound (ActiveRecord::RecordNotFound): friendly_id (5.0.4) lib/friendly_id/finder_methods.rb:23:in `find' activerecord (4.1.2) lib/active_record/querying.rb:3:in `find' cancancan (1.9.1) lib/cancan/model_adapters/abstract_adapter.rb:20:in `find' cancancan (1.9.1) lib/cancan/controller_resource.rb:116:in `find_resource' cancancan (1.9.1) lib/cancan/controller_resource.rb:68:in `load_resource_instance' cancancan (1.9.1) lib/cancan/controller_resource.rb:32:in `load_resource' cancancan (1.9.1) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource' cancancan (1.9.1) lib/cancan/controller_resource.rb:10:in `block in add_before_filter' activesupport (4.1.2) lib/active_support/callbacks.rb:440:in `instance_exec' activesupport (4.1.2) lib/active_support/callbacks.rb:440:in `block in make_lambda' activesupport (4.1.2) lib/active_support/callbacks.rb:143:in `call' activesupport (4.1.2) lib/active_support/callbacks.rb:143:in `block in halting_and_conditional' activesupport (4.1.2) lib/active_support/callbacks.rb:229:in `call' activesupport (4.1.2) lib/active_support/callbacks.rb:229:in `block in halting' activesupport (4.1.2) lib/active_support/callbacks.rb:149:in `call' activesupport (4.1.2) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional' activesupport (4.1.2) lib/active_support/callbacks.rb:229:in `call' activesupport (4.1.2) lib/active_support/callbacks.rb:229:in `block in halting' activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `call' activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `block in halting' activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `call' activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `block in halting' activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `call' activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `block in halting' activesupport (4.1.2) lib/active_support/callbacks.rb:86:in `call' activesupport (4.1.2) lib/active_support/callbacks.rb:86:in `run_callbacks' actionpack (4.1.2) lib/abstract_controller/callbacks.rb:19:in `process_action' actionpack (4.1.2) lib/action_controller/metal/rescue.rb:29:in `process_action' actionpack (4.1.2) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action' activesupport (4.1.2) lib/active_support/notifications.rb:159:in `block in instrument' activesupport (4.1.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument' activesupport (4.1.2) lib/active_support/notifications.rb:159:in `instrument' actionpack (4.1.2) lib/action_controller/metal/instrumentation.rb:30:in `process_action' actionpack (4.1.2) lib/action_controller/metal/params_wrapper.rb:250:in `process_action' activerecord (4.1.2) lib/active_record/railties/controller_runtime.rb:18:in `process_action' actionpack (4.1.2) lib/abstract_controller/base.rb:136:in `process' actionview (4.1.2) lib/action_view/rendering.rb:30:in `process' actionpack (4.1.2) lib/action_controller/metal.rb:196:in `dispatch' actionpack (4.1.2) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch' actionpack (4.1.2) lib/action_controller/metal.rb:232:in `block in action' actionpack (4.1.2) lib/action_dispatch/routing/route_set.rb:82:in `call' actionpack (4.1.2) lib/action_dispatch/routing/route_set.rb:82:in `dispatch' actionpack (4.1.2) lib/action_dispatch/routing/route_set.rb:50:in `call' actionpack (4.1.2) lib/action_dispatch/journey/router.rb:71:in `block in call' actionpack (4.1.2) lib/action_dispatch/journey/router.rb:59:in `each' actionpack (4.1.2) lib/action_dispatch/journey/router.rb:59:in `call' actionpack (4.1.2) lib/action_dispatch/routing/route_set.rb:678:in `call' railties (4.1.2) lib/rails/engine.rb:514:in `call' railties (4.1.2) lib/rails/railtie.rb:194:in `public_send' railties (4.1.2) lib/rails/railtie.rb:194:in `method_missing' actionpack (4.1.2) lib/action_dispatch/journey/router.rb:71:in `block in call' actionpack (4.1.2) lib/action_dispatch/journey/router.rb:59:in `each' actionpack (4.1.2) lib/action_dispatch/journey/router.rb:59:in `call' actionpack (4.1.2) lib/action_dispatch/routing/route_set.rb:678:in `call' warden (1.2.3) lib/warden/manager.rb:35:in `block in call' warden (1.2.3) lib/warden/manager.rb:34:in `catch' warden (1.2.3) lib/warden/manager.rb:34:in `call' rack (1.5.2) lib/rack/etag.rb:23:in `call' rack (1.5.2) lib/rack/conditionalget.rb:25:in `call' rack (1.5.2) lib/rack/head.rb:11:in `call' actionpack (4.1.2) lib/action_dispatch/middleware/params_parser.rb:27:in `call' actionpack (4.1.2) lib/action_dispatch/middleware/flash.rb:254:in `call' rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context' rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call' actionpack (4.1.2) lib/action_dispatch/middleware/cookies.rb:560:in `call' activerecord (4.1.2) lib/active_record/query_cache.rb:36:in `call' activerecord (4.1.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call' activerecord (4.1.2) lib/active_record/migration.rb:380:in `call' actionpack (4.1.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' activesupport (4.1.2) lib/active_support/callbacks.rb:82:in `run_callbacks' actionpack (4.1.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call' actionpack (4.1.2) lib/action_dispatch/middleware/reloader.rb:73:in `call' actionpack (4.1.2) lib/action_dispatch/middleware/remote_ip.rb:76:in `call' actionpack (4.1.2) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call' actionpack (4.1.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' railties (4.1.2) lib/rails/rack/logger.rb:38:in `call_app' railties (4.1.2) lib/rails/rack/logger.rb:20:in `block in call' activesupport (4.1.2) lib/active_support/tagged_logging.rb:68:in `block in tagged' activesupport (4.1.2) lib/active_support/tagged_logging.rb:26:in `tagged' activesupport (4.1.2) lib/active_support/tagged_logging.rb:68:in `tagged' railties (4.1.2) lib/rails/rack/logger.rb:20:in `call' actionpack (4.1.2) lib/action_dispatch/middleware/request_id.rb:21:in `call' rack (1.5.2) lib/rack/methodoverride.rb:21:in `call' rack (1.5.2) lib/rack/runtime.rb:17:in `call' activesupport (4.1.2) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call' rack (1.5.2) lib/rack/lock.rb:17:in `call' actionpack (4.1.2) lib/action_dispatch/middleware/static.rb:64:in `call' rack (1.5.2) lib/rack/sendfile.rb:112:in `call' railties (4.1.2) lib/rails/engine.rb:514:in `call' railties (4.1.2) lib/rails/application.rb:144:in `call' rack (1.5.2) lib/rack/lock.rb:17:in `call' rack (1.5.2) lib/rack/content_length.rb:14:in `call' rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service' /usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' /usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' /usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' 时:

rake db:seed

- 通过db/seeds.rb添加:

Forem::Category.create!(:name => 'General') user = User.create( :email => "admin@example.com", :password => "admin1234" ) user.forem_admin = true user.save! user.update_attribute(:forem_state, 'approved') unless user.nil? forum = Forem::Forum.create(:category_id => Forem::Category.first.id, :name => "Default", :description => "Default forem created by install") topic1 = forum.topics.build({ :subject => "Apples", :posts_attributes => [:text => "Eat apples"] }) topic1.user = user topic1.save! topic2 = forum.topics.build({ :subject => "Bananas", :posts_attributes => [:text => "Eat bananas"] }) topic2.user = user topic2.save! topic3 = forum.topics.build({ :subject => "Oranges", :posts_attributes => [:text => "Eat oranges"] }) topic3.user = user topic3.save! topic4 = forum.topics.build({ :subject => "Mangos", :posts_attributes => [:text => "Eat mangos"] }) topic4.user = user topic4.save! end

{{1}}

1 个答案:

答案 0 :(得分:1)

日志输出显示调用Forem::ForumsController#show操作而不是搜索:

Started GET "/search?utf8=%E2%9C%93&keywords=Mangos" at 2014-10-17 19:33:15 +0000
Processing by Forem::ForumsController#show as HTML

您的搜索路线被Forem引擎路线覆盖,特别是this resourceful route attached to root

resources :forums, :only => [:index, :show], :path => "/" do
  # ...snipped...

/映射到论坛索引,/search映射到论坛节目(ID:“搜索”,如记录的SQL输出中所示)。


要解决此问题,将您的“搜索”路线移到Forem引擎上方,因为路线从上到下排列优先顺序:

App::Application.routes.draw do
  get '/search', to: 'forem/forums#search', as: 'search'
  mount Forem::Engine, :at => '/'
  devise_for :users
end