views:

270

answers:

3

New to rails...so bare with me.

I successfully installed and set it up searchlogic for basic (keyword) searching. I have the following problem:

@search = Proposal.search(params[:search])
@proposals = @search.all

The above code works properly if I type in a keyword such as "red". It will bring up everything with red keyword. Or if I type in "green", it will bring up everything with green as a keyword. However, when I type "red green" in the search box it will ONLY bring up cases where the keywords are BOTH red and green (and not bring up instances where they may only have one of the two keywords). Yes, I am using keywords_like_any. I can see what the general problem is via debug, keywords_like_any: green red. The below code works as I want it to (bring up any instances of red OR green).

@search2 = Proposal.keywords_like_any("red", "green")
@test = @search2.all

So I believe what I need to do to solve the issue is turn the first code to view params[:search] as an array? I tried doing params[:string].to_s.split (as shown in railscast) however it did not work.

If someone can point me in the right direction, I would appreciate that.

A: 

You said you tried params[:string].to_s.split - I don't know if that's a typo, but it should be params[:search].to_s.split

Andy Gaskell
A: 

The *_like_any is intended to be used with checkboxes form helpers (f.check_box) which outputs arrays into your params hash as opposed to f.text_field which outputs strings. If you still want to use them with a f.text_field you can :

  • split the field with Javascript on client side to output an Array
  • split it in your controller

Considering that your field is named keyboard here is some code that should solve your problem :

params[:search][:keywords_like_any] = params[:search][:keywords_like_any].split(' ')
@proposals = Proposal.search(params[:search])

You can skip the line @proposals = @search.all because search results works like an array.

jhc_
This solves my main problem, thanks a ton! Although it left me with a small quirk. Now when I search, after the page is refreshed my searchbox is filled with "redgreen" instead of "red green". Odd?
MrCheetoDust
Since you use a `form_for, it fills the `text_field`with the value you previously inputted ( which ended in your search object ). But this value is an array, so when converted in a string. `['red', 'green']`=> `"redgreen". @proposals.keywords_like_any = @proposals.keywords_like_any.join(" ")To keep the code readable, I'd rename the `@proposals` variable back to `@search`, it does not really make sense if it's not the case. ( and thus completing with `@proposals = @search.all` ).
jhc_
A: 

I may not understand your question, but it appears to me that you're trying to implement something in the controller that belongs in the view. If your view has this:

- form_for @search do |f|
  = f.text_field :color_like_any

then your initial controller example will work.

Gavin
My view does have that code...and it does 'work', as mentioned above. However, just not in the fashion I expected / wanted it to.
MrCheetoDust