Keyword searching defaulting to AND - ruby-on-rails

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
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.

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.

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

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.

Related

How to clear the search results in a Rails app?

I'm a Rails noob, and am looking for assistance in clearing the results of a search.
I've built a sample application which lists cafes, and have implemented searching using elasticsearch and searchkick. The search function appends parameters to the URL as in http://localhost:3000/cafes?search=sydney&commit=Search and is working correctly.
Now I'd like to add a button to clear the search results:
The Clear button's link is to the cafes_path (http://localhost:3000/cafes), so it basically reloads the page without the search parameters:
= form_tag cafes_path, :method => :get do
= text_field_tag :search
= submit_tag value = "Search"
= link_to cafes_path
Is that an acceptable way to clear the search?
Is it an acceptable Rails approach to use JavaScript logic to enable/disable the Clear button, based on what's in the search box?
Please let me know if you need to see more of the model or controller code to make sense of the question.
This way is pretty standard
sure, use javascript - or you can also google for other ways it's been done and see what they did.

Raw and simple_form...neither is perfect

I have this in one of my models:
def rank_match_2
...
...
array = [start, finish, [words]]
results = field.insert(start, "<mark>") and field.insert(finish, "</mark>")
...
end
Now I know I shouldn't be adding view logic in the model, but I am building a search app that has quite a bit of logic built into how the results are rendered. At this point I don't see how to get around including it in the model, so I that's what I am doing.
My problem is this:
On the view, I have this:
<%=raw #parse.rank_match_2 %>
This effectively handles the html "mark" tags inserted in the model logic and DOES highlight the correct text, but DOES NOT include line breaks etc.
However, this:
<%= simple_form(#parse.rank_match_2).html_safe %>
does not handle the 'mark' tags and therefore DOES NOT highlight the correct text, but DOES correctly format the line breaks as expected.
I want to do both: highlight the correct text by inserting the 'mark' tags into the model object (which seems to work with 'raw'), AND render correctly formatted html with line breaks etc.
Any idea what I am missing. I am trying Draper gem but I don't think it is suitable for exactly what I want.

Adding forms in non-form pages in ActiveAdmin

I'm trying to add a simple select box and submit button to a "show" page in ActiveAdmin. Basically, the clients wants a simple way to assign a currently unassigned widget to the item currently being viewed. Not that that really matters.
What I am seeing is that although I can add a form and a select box, if I try to add anything after the select, the select doesn't get displayed. It's not that it is hidden by CSS, but that it just doesn't render.
Here's the relevant code:
column do
panel "Devices without locations" do
devices = Device.without_location
form_tag add_device_admin_location_path do
select_tag(:device_id, options_from_collection_for_select(devices, :id, :name))
submit_tag
end
end
end
The submit tag will be displayed, but the select will not. event if I put "foo" in there, only the "foo" will show up. The only time the select will show up is if there is nothing else in the block.
Update:
Okay, so I've been able to work around this by concatenating the output together. It's not ideal, and I definitely feel dirty, but it works.
I tried using formtastic on this, but it appears to only accepts attributes from the model, this doesn't work: I'm updating the device, not the location.
This works, but if anyone has a more better way of doing this, I'd love to know.
i had the same issue, and moving form into app/views/admin/#{model_name}s/_#{partial_name}.html.erb worked for me fine

Iterating through website links and clicking them

#editLinks = $ie.link(:text => "Edit", :class =>"edit" ).links
#editLinks.each do |l| l.click
end
I have the above code which iterates through a set of links which are called "Edit". When edit is clicked on the webpage, the text boxes become enabled and the user is able to type into them. There are different edit links for different textboxes. However, for some reason, the code is not iterating or giving me any errors. It just finishes the script without clicking any edit links. I've omitted the javascript onlcick stuff to show just the html. All the edit links on the webpage also have different ID's.
<a id="EditLinkButton" class="edit"><strong>Edit</strong></a>
After reading Get Elements By Attributes question, I'd suggest doing something like the following:
#editLinks = $ie.links
#editLinks.each do |link|
if link.attribute_value("class") == "edit" and link.text == "Edit"
link.click
end
end
There's probably a better way of doing this, but it works.
Edit:
After reading your comment below, perhaps something along the lines of this would be better
#edit_ids = %w(your edit link ids seperated by whitespace)
#edit_ids.each do |edit_id|
$ie.link(:id => edit_id).click
# whatever else you're doing
end
So that way you're looking them up in the DOM as and when you need them, rather than storing them and then their references becoming obsolete as the page changes during the test. Worth a shot.

how to write query string for webrick?

My rails application requires few values to be specified in the text box.
My web page contains few text boxes .How can i specify the values of these text boxes in the url as query string while using webrick?can any one help, am new to this.
Thanks in advance.
If your url looks like this: localhost:3000/Accounts/1/edit?value1=A&value2=B and you want to put those in text boxes you must create some instance variables in the controller, and then reference them in the view.
Controller Action:
def edit
#value1 = params[:value1]
#value2 = params[:value2]
end
View:
<%= text_box_tag :value1, #value1 %>
<%= text_box_tag :value2, #value2 %>
If you followed my example, the first text box would display A and the second B.
Note that the webserver has no effect on this behavior. webrick, apache, mongrel, thin, etc... will all do this.

Resources