Sunspot/Solr raketasks未在Rails 3可安装引擎中加载

问题描述:

我正在尝试将 sunspot_rails 宝石添加到我的

I'm trying to add the sunspot_rails gem to my Rails Mountable Engine, so I can use Solr to do full text searches. Like it states in the README file I've added this to my Gemfile:

gem "sunspot_rails"
gem "sunspot_solr"

然后运行rails g sunspot_rails:install,它会在Rails Engine的config文件夹中创建sunspot.yml.要启动黑子,我需要运行:

Then I run rails g sunspot_rails:install which creates sunspot.yml in the config folder of my Rails Engine. To start sunspot I need to run:

bundle exec rake sunspot:solr:start

但这不起作用,并给我以下错误消息:

But that doesn't work, and gives me the following error message:

rake aborted!
Don't know how to build task 'sunspot:solr:start'

似乎它没有加载Sunspot/Solr rake任务,因此找不到它们.我还遇到了其他问题,Rails引擎无法加载将在正常Rails 3应用程序中自动加载的文件.我怀疑沿着这条线的事情也在发生.我检查了lib/tasks文件夹,并且那里只有一个文件:my_app.rake.在该文件中,只有几行注释行代码:

It seems that it doesn't load the Sunspot/Solr rake tasks, and so it can't find them. I've had other issues with the Rails engine not loading files that would load automatically in a normal Rails 3 application. I suspect something along that line is going on here as well. I checked the lib/tasks folder, and there is only one file in there: my_app.rake. In that file there are only a few commented lines of code:

# desc "Explaining what the task does"
# task :my_app do
#   # Task goes here
# end

我认为我可能需要手动添加raketasks,然后从my_app.rake加载它们.但是,我找不到有关如何执行此操作的任何信息,也许我一开始就完全错了.希望有人在stackoverflow上有经验.

I think I would maybe need to add the raketasks manually, and load them from my_app.rake. However, I can't find any information on how to do this, and I maybe I'm completely wrong in the first place. Hopefully someone on stackoverflow has experience with this.

无论如何,非常感谢您提前提供的帮助!

In any case, thank you very much for any help in advance!

几天前我自己找到了解决此问题的方法,因此为了完整起见,并(希望)对他人有所帮助,我将发布解决方案在这里.

A few days ago found a solution to this problem myself, so for the sake of completeness and (hopefully) to be of help to others, I will post my solution here.

实际上发生此问题是因为Rails 3可安装引擎的行为与正常的Rails应用类似,但也有许多次要/主要差异.您只需要运行bundle exec rake app:sunspot:solr:start而不是bundle exec rake sunspot:solr:start.

The problem actually occurs because a Rails 3 Mountable Engine behaves a lot LIKE a normal Rails app, but also has a lot of minor/major differences. You just have to run bundle exec rake app:sunspot:solr:start instead of just bundle exec rake sunspot:solr:start.

因此,要使Sunspot在Rails 3可安装引擎中工作,请按照以下步骤操作:

So to get Sunspot working in a Rails 3 Mountable Engine, I followed these steps:

添加到Gemfile

# Gemfile (Don't forget to move them to your gemspec when packaging your engine)

gem 'sunspot_solr' #only for development mode
gen 'sunspot_rails'

之后,请不要忘记运行bundle install来安装gem.

After that, don't forget to run bundle install to install the gems.

配置引擎以使用Sunspot/Solr

要生成所需的config/sunspot.yml文件,请运行:

To generate the needed config/sunspot.yml file run:

rails generate sunspot_rails:install

在本地计算机上启动Solr

最后,要启动Solr的本地实例,请运行:

Finally, to start a local instance of Solr run:

bundle exec rake app:sunspot:solr:start

现在您已经准备就绪!