I am beginning to think that mt brain doesnt speak Ruby(or rails)!
I am attempting to build the most basic search function imaginable -
Search within the database for a particular location
Controller
@locations = Location.find(:all, :conditions => ["location like ?",
"%#{@search_text}%"])
In my view I just put my basic scaffolded view from show, reasoing that
this should return that exact result
View
<h2>Find Results</h2>
<% for column in Location.content_columns %>
<p>
<b><%= column.human_name %>:</b> <%=h @location.send(column.name) %>
</p>
<% end %>
Help how do I resolve basic results like this? More properly I know it
should return a table of all the close matches, but anything other than
nasty NOMethoError# would be great!
First of, this find looks much cleaner and is more scure as everything
is escaped by Rails:
@locations = Location.find(:all, :conditions => ["location like %?%",
@search_text])
For the view: forget that scaffold stuff, it keeps you from learning
how to do it yourself.
in your current view, you loop through the Models *column headers* and
not the returned results in @location.
<%=h @location.send(column.name) %> will therefore not return each
results content, but throw an error i guess ... not too fit in ruby
myself, but been i while since touched scaffold for the reason
mentioned above.
Here is where I am with this now.
The 'Location like %?%....'
Would result in a MySQL error like
Mysql::Error: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near '%'Hicom Room'%)'
So the wildcards are on the wrong side of the quotes
If I hard code
@search_text = 'Hicom Room' and take the wildcards out it works fine
So... one step close I guess!
This turned out to be the cause of the Mysql error below as it seemed
to be adding whitespace and new line characters,
as soon as I changed the filed to be :