2012-03-09 72 views

回答

1

你需要用任何你想要的参数来调用搜索。相反的:

def index 
    @search = Article.search(params[:search]) 
    @articles = @search.all # load all matching records 
end 

你可能有

def show 
    @search = Article.search(params[:search]) 
    @articles = @search.where(:user_id => params [:user_id]).all # load all matching records 
end 

根据自定义Where创建

+0

好的。但我的搜索表单是在应用程序布局,所以我必须在应用程序控制器上过滤索引电影:'def index_movies @search = Movie.search(params [:search]) @movies = @ search.all end “我只是采取了错误的做法? (对不起,我无法格式化!) – fertech 2012-03-15 16:45:04

+0

您的搜索在'application.html.erb'? – ScottJShea 2012-03-15 16:55:00

+0

是的,我有一个在application.html.erb – fertech 2012-03-15 17:43:35

0

如果你想搜索行动,使用不同的路线,给url选项form_for

<%= form_for @search, :url => search_results_movies_path do |f| %>

+0

嗯,我有这在我的应用程序布局上:'<%= form_for @search,search_results_movies_path do | f | %> <%= f.text_field:title_or_actors_or_director_contains,:class =>“span2”%> <% end %>'现在,如果我在与”电影“不同的控制器中开始搜索,它不会重定向到搜索结果。我能解决这个问题吗? – fertech 2012-03-15 16:31:26

+0

@fertech你必须通过选项哈希传递网址,如下所示:<%= form_for @search,:url => search_results_movies_path do | f | %>' – graywh 2012-12-12 16:17:31