Thinking_Sphinx 在控制台中工作,但不在 Rails 应用程序中工作

发布于 2024-10-22 06:40:45 字数 2710 浏览 0 评论 0原文

环境 Rails 3.0.4、Ruby 1.8.7、Sphinx 0.99、Dreamhost VPS

我有一个简单的博客搜索设置如下:

控制器:

def blog_search_results     
    @blog_search_results = BlogPost.search( params[:search_param] )
    respond_to do |format| 
      format.html { render :partial => 'blog_search_results', :layout=>false }
      format.xml  { render :xml     => @blog_search_results }
    end     
end

视图:

<% if !@blog_search_results.empty? %>
    <div id="main_content">
        <table id="pending_blog_list"><tr id="header"><td>Blog Title</td><td>Created by</td><td>Created on</td></tr>
        <% @blog_search_results.each do |blog_post| %>
            <tr>
                <td><%= link_to blog_post.title, blog_named_link(blog_post, :show) %></td>
                <td><%= blog_post.posted_by.display_name -%></td>
                <td><%= blog_post.created_at.strftime("%x") -%></td>
            </tr>
        <% end %>
        </table>
        <%= will_paginate(@blog_search_results, {:form_name => :blog_show_params, :param_name => :page} ) %>
    </div>
<% else %>
    <h2>No blog posts that match your search were found</h2>
<% end %>

一切都在我的开发环境中完美运行。在生产环境中,当我启动控制台(“rails c production”)并键入:

irb(主):003:0> BlogPost.search(“视频”)

我得到了预期的 4 个带有“video”一词的帖子结果。然而,在实际的应用程序中,控制器不断返回“404 Not Found”。

Started GET "/blog_posts/blog_search_results?search_param=video" 
Processing by BlogPostsController#blog_search_resul as HTML   
Parameters: {"search_param"=>"video"} 
Completed in 27ms 
Rendered layouts/application.html.erb (22.3ms)
Rendered public/404.html within layouts/blog_posts (24.7ms)

我的 sphinx.yml 设置如下:

staging:
  pid_file: '/www.assetcorr.com/current/log/searchd.pid'
  searchd_file_path: 'www.assetcorr.com/shared/db/sphinx'
  bin_path: '/home/avitus/local/bin'

我在共享/db/sphinx 文件夹中看到索引。它在我运行 ts:index.html 之后出现。 searchd.pid 文件位于日志文件夹中,并且 PID 与 searchd 守护进程匹配。 bin_path 是正确的,我已将其添加到 .bash_profile 文件中的路径中。

有谁知道可能导致此问题的原因吗?

-- 更新 --

看起来这不起作用的原因是控制器方法是通过 AJAX 调用访问的:

$(document).ready(function() {
  $("#search_param").observe_field(0.5, function( ) { 
    $.get("/blog_posts/blog_search_results", { search_param: this.value },
      function(data){   
    $("#search_results").html(data);
      }, "html");
    });
  });
});

当我删除 AJAX 调用并诉诸普通的 GET 请求时,它可以完美地工作。我不知道为什么会这样。

Environment Rails 3.0.4, Ruby 1.8.7, Sphinx 0.99, Dreamhost VPS

I have a simple blog search set up as follows:

Controller:

def blog_search_results     
    @blog_search_results = BlogPost.search( params[:search_param] )
    respond_to do |format| 
      format.html { render :partial => 'blog_search_results', :layout=>false }
      format.xml  { render :xml     => @blog_search_results }
    end     
end

View:

<% if !@blog_search_results.empty? %>
    <div id="main_content">
        <table id="pending_blog_list"><tr id="header"><td>Blog Title</td><td>Created by</td><td>Created on</td></tr>
        <% @blog_search_results.each do |blog_post| %>
            <tr>
                <td><%= link_to blog_post.title, blog_named_link(blog_post, :show) %></td>
                <td><%= blog_post.posted_by.display_name -%></td>
                <td><%= blog_post.created_at.strftime("%x") -%></td>
            </tr>
        <% end %>
        </table>
        <%= will_paginate(@blog_search_results, {:form_name => :blog_show_params, :param_name => :page} ) %>
    </div>
<% else %>
    <h2>No blog posts that match your search were found</h2>
<% end %>

Everything works perfectly in my development environment. In the production environment, when I launch a console ("rails c production") and type:

irb(main):003:0>
BlogPost.search("video")

I get the expected 4 results of posts with the word 'video'. However in the actual app, the controller keeps returning a '404 Not Found'

Started GET "/blog_posts/blog_search_results?search_param=video" 
Processing by BlogPostsController#blog_search_resul as HTML   
Parameters: {"search_param"=>"video"} 
Completed in 27ms 
Rendered layouts/application.html.erb (22.3ms)
Rendered public/404.html within layouts/blog_posts (24.7ms)

My sphinx.yml setup is as follows:

staging:
  pid_file: '/www.assetcorr.com/current/log/searchd.pid'
  searchd_file_path: 'www.assetcorr.com/shared/db/sphinx'
  bin_path: '/home/avitus/local/bin'

I see the index in the in the shared/db/sphinx folder. It appears after I run ts:index.
The searchd.pid file is in the log folder and the PID matches the searchd daemon.
The bin_path is correct and I've added it to the path in the .bash_profile file.

Does anyone have any ideas what could be causing this?

-- UPDATE --

It look as though the reason this isn't working is that the controller method is being accessed via an AJAX call:

$(document).ready(function() {
  $("#search_param").observe_field(0.5, function( ) { 
    $.get("/blog_posts/blog_search_results", { search_param: this.value },
      function(data){   
    $("#search_results").html(data);
      }, "html");
    });
  });
});

When I remove the AJAX call and resort to a plain vanilla GET request then it works perfectly. I have no idea why that would be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯角 2024-10-29 06:40:45

问题在于思考狮身人面像的路径。我希望你正在运行 Passenger。请在 sphinx.yml 中指定路径(您的 sphinx 配置 yml 文件是什么)

The problem is with the path of the Thinking Sphinx. I hope you are running on Passenger. please specify the path in sphinx.yml(what ever is your sphinx config yml file)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文