2014-10-17 27 views
0

我想回形針添加到Forem-based Rails app添加PgSearch到Rails應用程序:`的ActiveRecord :: RecordNotFound`

直播應用:http://runnable.com/VEFe5hZXSE9z0q5x/forem-with-pgsearch-for-ruby-on-rails(擊中Run按鈕之前運行/etc/init.d/postgresql start

但爲什麼儘管搜索存在的帖子,我是不是在收到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 => "[email protected]", 
    :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

哇,與downvotes什麼? – 2014-10-17 23:29:26

+0

這似乎是一個路由問題(您的'搜索'路線被覆蓋)。發佈您的路線文件。 – Substantial 2014-10-17 23:54:21

+0

嗨!謝啦。只是這個:https://gist.github.com/anonymous/6aff4926bdba9b99e571 – 2014-10-17 23:59:45

回答

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 

你的搜索路徑正在被培訓就業部引擎路線覆蓋,特別是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 
+0

不錯!這似乎已經做到了,但是現在我面臨着Forem :: Forums#search中的ActionController :: UrlGenerationError(請參閱更新後的實時沙盒應用程序)。你知道這是否相關嗎? – 2014-10-18 00:38:35

+1

在** app/views/forem/topics/_topic.html.erb:20 **路由幫助參數爲零。 '@ forum'通常由[此私有控制器方法](https://github.com/radar/forem/blob/rails4/app/controllers/forem/topics_controller.rb#L98)定義,但當前的搜索處理結果不會使用它並留下'@ forum'nil。髒修復:用'(@forum || topic.forum_id)'替換'@ forum';這將使搜索工作暫時停止,但它將繞過查看計數和「讀取」授權(向未授權閱讀的用戶列出主題),因此請勿將其用於生產。 – Substantial 2014-10-18 06:32:33

+0

非常感謝你真實!骯髒的修復是現在好:) – 2014-10-18 11:30:41

相關問題