rails 3.2 搜索动作。如何将结果传递给另一个动作
rails 3.2 searching action. how to pass results to another action
我应该创建一个搜索页面,我必须在其中将所有搜索结果保存在一个数组中。我有两个问题:
1) 我使用了以下语句:
Company.joins(:references).where(sql_string)
returns 和 ActiveRecord:Relation
对我不利,因为我必须在索引操作中显示这些结果,其中我使用 each
语句。所以,为了克服这个问题,我使用了 to_a
。
我检查了变量的 .class
,并使用 to_a
从 ActiveRecord:Relation
传递到 Array
。所以,这似乎解决了问题。
Company.joins(:references).where(sql_string).to_a
2) 现在,我必须将这个变量 (Array
) 传递到我的 index
操作中。
我在名为 search
:
的操作中执行了搜索
def search
...
@companies = Company.joins(:references).where(sql_string).to_a
end
现在,我想将其传递给 index
:
def index
@companies ||= Company.all
end
我使用了 @companies ||= Company.all
因为我认为 @companies
是一个距离变量,它应该在 class 的所有操作中可用。不是吗?顺便说一句,它不起作用。我的意思是,结果不会通过这两种方法共享。
此外,在 search
操作中我不知道如何调用 index
操作。我使用了 redirect_to
但这给我带来了另一个问题。
def search
...
@companies = Company.joins(:references).where(sql_string).to_a
redirect_to companies_index_path
end
我第二次调用搜索操作时,它把我带到了索引中 action.As 我插入了搜索值。实际上他仍然在记忆中搜索过去,我不想要这种行为。
所以,换句话说,我想:
正在将@companies 搜索结果传递给索引操作。
避免search-index
之间的循环。所以在每个新请求重置
老搜
我想知道 to_a
的转换是否正确
ActiveRecord:Relation
到 Array
.
谢谢。
编辑:
def search
stringa_sql = ""
ragione_sociale = ""
riferimento = ""
note = ""
min_date = ""
max_date = ""
company_type = ""
sector = ""
country = ""
certification = ""
contact = ""
state = ""
manage = ""
consultation = ""
formation = ""
software = ""
if params[:ragione_sociale]
ragione_sociale = params[:ragione_sociale]
stringa_sql = "ragione_sociale like "+"'%"+ragione_sociale+"%'"
end
if params[:riferimento]
riferimento = params[:riferimento]
stringa_sql += " AND nome like "+"'%"+riferimento+"%'"
end
if params[:note]
note = params[:note]
stringa_sql += " AND note like "+"'%"+note+"%'"
end
if params[:min_date] && params[:min_date]!= ""
if params[:max_date] && params[:max_date]!= ""
min_date = params[:min_date]
max_date = params[:max_date]
stringa_sql += " AND richiamare >="+min_date+" AND richiamare <="+max_date
end
end
if params[:company_type] #se inviamo la richesta senza scrivere params[:category] viene passato vuoto
if params[:company_type][:id] != ""
company_type = params[:company_type][:id]
stringa_sql += " AND id_forma_giuridica = "+company_type
end
end
if params[:sector]
if params[:sector][:id] != ""
sector = params[:sector][:id]
stringa_sql += " AND id_settore = "+sector
end
end
if params[:country]
if params[:country][:id] != ""
country = params[:country][:id]
stringa_sql += " AND id_provincia = "+country
end
end
if params[:certification]
if params[:certification][:id] != ""
certification = params[:certification][:id]
stringa_sql += " AND id_certificazione = "+certification
end
end
if params[:contact]
if params[:contact][:id] != ""
contact = params[:contact][:id]
stringa_sql += " AND id_contattato = "+contact
end
end
if params[:state]
if params[:state][:id] != ""
state = params[:state][:id]
stringa_sql += " AND id_stato = "+state
end
end
if params[:manage]
if params[:manage][:id] != ""
manage = params[:manage][:id]
stringa_sql += " AND id_gestito = "+manage
end
end
if params[:consultation]
if params[:consultation][:id] != ""
consultation = params[:consultation][:id]
stringa_sql += " AND id_consulenza = "+consultation
end
end
if params[:formation]
if params[:formation][:id] != ""
formation = params[:formation][:id]
#formazione DA METTERE
end
end
if params[:software]
if params[:software][:id] != ""
software = params[:software][:id]
stringa_sql += " AND id_software = "+software
end
end
@companies = Company.search(stringa_sql).to_a
if not @companies.empty?
redirect_to companies_index_path
end
end
索引:
def index
@companies ||= Company.all
end
I used @companies ||= Company.all cause i think that the @companies
is and istance variable and it should be available in all the actions
of the class. Isn't it?
不一定,这取决于您想从何处访问 @companies
实例变量。例如从哪个角度来看,控制器对应的action方法中需要@companies
个实例变量
The second time i call the search action it brings me into the index
action
您正在 search
方法中使用 redirect_to companies_index_path
,这会将您带到索引操作。
要在您的应用程序中实现搜索,您可以遵循以下标准流程:
在你的 application_controller.rb
中会有 @search_query
.
# Returns string
def search_query
@search_query ||= params[:query] || params[:search]
end
然后,在您的 searches_controller.rb
中,您可以拥有:
def search
# in the method build your search results based on
# search_query param
@search_results = Company.joins(:references).where(sql_string(search_query)).to_a
end
在您的 routes.rb
中,您可以拥有:
get '/search(/:query)' => 'searches#search', query: /.+/, as: 'search'
您将转到 searches_controller 的 search
操作,您正在其中构建搜索结果 @search_results
。
最后,您需要一个 app/views/searches/search.html.erb
视图文件,您可以在其中访问您的 @search_results
实例变量,您可以循环遍历它们并在此视图中显示它们。
最后 3 个问题的答案:
passing @companies searching result to index action.
avoid the loop between search-index. So in every new request resets
the old searching.
您可以按照我上面提到的 request/response 流程克服这些问题。您不应与 search
共享您的 index
视图,并且您不应在 search
和 index
之间有任何循环。它们都是控制器的独立动作,可以像我上面显示的那样单独处理。
- i want to know if it's correct the casting with the to_a to bring an
ActiveRecord:Relation to Array.
如果你愿意,你可以这样做。但是,在此用例中您并不真正需要它。您可以将 ActiveRecord:Relation
存储在 search_results
中,当您从 search.html.erb
视图中访问该实例变量时,您可以使用 .each do
块轻松循环。所以,你不必担心 ActiveRecord:Relation
和 Array
.
我应该创建一个搜索页面,我必须在其中将所有搜索结果保存在一个数组中。我有两个问题:
1) 我使用了以下语句:
Company.joins(:references).where(sql_string)
returns 和 ActiveRecord:Relation
对我不利,因为我必须在索引操作中显示这些结果,其中我使用 each
语句。所以,为了克服这个问题,我使用了 to_a
。
我检查了变量的 .class
,并使用 to_a
从 ActiveRecord:Relation
传递到 Array
。所以,这似乎解决了问题。
Company.joins(:references).where(sql_string).to_a
2) 现在,我必须将这个变量 (Array
) 传递到我的 index
操作中。
我在名为 search
:
def search
...
@companies = Company.joins(:references).where(sql_string).to_a
end
现在,我想将其传递给 index
:
def index
@companies ||= Company.all
end
我使用了 @companies ||= Company.all
因为我认为 @companies
是一个距离变量,它应该在 class 的所有操作中可用。不是吗?顺便说一句,它不起作用。我的意思是,结果不会通过这两种方法共享。
此外,在 search
操作中我不知道如何调用 index
操作。我使用了 redirect_to
但这给我带来了另一个问题。
def search
...
@companies = Company.joins(:references).where(sql_string).to_a
redirect_to companies_index_path
end
我第二次调用搜索操作时,它把我带到了索引中 action.As 我插入了搜索值。实际上他仍然在记忆中搜索过去,我不想要这种行为。 所以,换句话说,我想:
正在将@companies 搜索结果传递给索引操作。
避免
search-index
之间的循环。所以在每个新请求重置 老搜我想知道
to_a
的转换是否正确ActiveRecord:Relation
到Array
.
谢谢。
编辑:
def search
stringa_sql = ""
ragione_sociale = ""
riferimento = ""
note = ""
min_date = ""
max_date = ""
company_type = ""
sector = ""
country = ""
certification = ""
contact = ""
state = ""
manage = ""
consultation = ""
formation = ""
software = ""
if params[:ragione_sociale]
ragione_sociale = params[:ragione_sociale]
stringa_sql = "ragione_sociale like "+"'%"+ragione_sociale+"%'"
end
if params[:riferimento]
riferimento = params[:riferimento]
stringa_sql += " AND nome like "+"'%"+riferimento+"%'"
end
if params[:note]
note = params[:note]
stringa_sql += " AND note like "+"'%"+note+"%'"
end
if params[:min_date] && params[:min_date]!= ""
if params[:max_date] && params[:max_date]!= ""
min_date = params[:min_date]
max_date = params[:max_date]
stringa_sql += " AND richiamare >="+min_date+" AND richiamare <="+max_date
end
end
if params[:company_type] #se inviamo la richesta senza scrivere params[:category] viene passato vuoto
if params[:company_type][:id] != ""
company_type = params[:company_type][:id]
stringa_sql += " AND id_forma_giuridica = "+company_type
end
end
if params[:sector]
if params[:sector][:id] != ""
sector = params[:sector][:id]
stringa_sql += " AND id_settore = "+sector
end
end
if params[:country]
if params[:country][:id] != ""
country = params[:country][:id]
stringa_sql += " AND id_provincia = "+country
end
end
if params[:certification]
if params[:certification][:id] != ""
certification = params[:certification][:id]
stringa_sql += " AND id_certificazione = "+certification
end
end
if params[:contact]
if params[:contact][:id] != ""
contact = params[:contact][:id]
stringa_sql += " AND id_contattato = "+contact
end
end
if params[:state]
if params[:state][:id] != ""
state = params[:state][:id]
stringa_sql += " AND id_stato = "+state
end
end
if params[:manage]
if params[:manage][:id] != ""
manage = params[:manage][:id]
stringa_sql += " AND id_gestito = "+manage
end
end
if params[:consultation]
if params[:consultation][:id] != ""
consultation = params[:consultation][:id]
stringa_sql += " AND id_consulenza = "+consultation
end
end
if params[:formation]
if params[:formation][:id] != ""
formation = params[:formation][:id]
#formazione DA METTERE
end
end
if params[:software]
if params[:software][:id] != ""
software = params[:software][:id]
stringa_sql += " AND id_software = "+software
end
end
@companies = Company.search(stringa_sql).to_a
if not @companies.empty?
redirect_to companies_index_path
end
end
索引:
def index
@companies ||= Company.all
end
I used @companies ||= Company.all cause i think that the @companies is and istance variable and it should be available in all the actions of the class. Isn't it?
不一定,这取决于您想从何处访问 @companies
实例变量。例如从哪个角度来看,控制器对应的action方法中需要@companies
个实例变量
The second time i call the search action it brings me into the index action
您正在 search
方法中使用 redirect_to companies_index_path
,这会将您带到索引操作。
要在您的应用程序中实现搜索,您可以遵循以下标准流程:
在你的 application_controller.rb
中会有 @search_query
.
# Returns string
def search_query
@search_query ||= params[:query] || params[:search]
end
然后,在您的 searches_controller.rb
中,您可以拥有:
def search
# in the method build your search results based on
# search_query param
@search_results = Company.joins(:references).where(sql_string(search_query)).to_a
end
在您的 routes.rb
中,您可以拥有:
get '/search(/:query)' => 'searches#search', query: /.+/, as: 'search'
您将转到 searches_controller 的 search
操作,您正在其中构建搜索结果 @search_results
。
最后,您需要一个 app/views/searches/search.html.erb
视图文件,您可以在其中访问您的 @search_results
实例变量,您可以循环遍历它们并在此视图中显示它们。
最后 3 个问题的答案:
passing @companies searching result to index action.
avoid the loop between search-index. So in every new request resets the old searching.
您可以按照我上面提到的 request/response 流程克服这些问题。您不应与 search
共享您的 index
视图,并且您不应在 search
和 index
之间有任何循环。它们都是控制器的独立动作,可以像我上面显示的那样单独处理。
- i want to know if it's correct the casting with the to_a to bring an ActiveRecord:Relation to Array.
如果你愿意,你可以这样做。但是,在此用例中您并不真正需要它。您可以将 ActiveRecord:Relation
存储在 search_results
中,当您从 search.html.erb
视图中访问该实例变量时,您可以使用 .each do
块轻松循环。所以,你不必担心 ActiveRecord:Relation
和 Array
.