Hi,I am trying to implement a small live search thing in typo. the view
that I interative with is view/admin/content/_form.rhtml I created a
search field when creating an article and use observe_field to observe
this search field and tells the content_controller to search in database
for the appropriate match.
But when I tried to type a letter in the search field, I’ve got the
following error.
NoMethodError in Admin/content#search
Showing app/views/admin/content/search.rhtml where line #1 raised:
You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.length
Extracted source (around line #1):
1: <% if @results.length > 0 && @q != ‘’-%>
2:
3:
4:
RAILS_ROOT: ./script/…/config/…
Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/app/views/admin/content/search.rhtml:1:in
`_run_rhtml_admin_content_search’
anybody has some ideas? Thanks in advance!
|
The source code shows as follows
app/views/admin/content/_form.rhtml
<%= text_field_tag(:search) %>
<div id=search_indicator_div
style="postition: absolute; left: your_X px; top: your_Y px">
<%= image_tag("/images/spinner.gif", :id => 'indicator_gif_id',
:style => 'display:none') %>
</div>
<%= observe_field(:search,
:frequency => 0.5,
:update => :results,
:url => { :action => :search },
:loading => "Element.show('indicator_gif_id')",
:complete => "Element.hide('indicator_gif_id')") %>
app/controllers/admin/content_controller.rb
def search
@q = request.raw_post
# optionally handle the query however you’d like
# fetch some results from a database
@results = Tlc.find(:all, :conditions => [ “loc LIKE ?”, “%” + @q
+"%"])
end
whereas Tlc is a table that I created and loc is one column in this
table.
app/views/admin/content/search.rhtml
<% if @results.length > 0 && @q != ‘’-%>
Location
|
State
|
<% for result in @results %>
<tr>
<td align=center>
<%= result.loc -%><br>
</td>
<td>
<%= result.st -%>
</td>
</tr>
<% end %>
<% else -%>
<% if @q != ‘’ -%>
Sorry, there are no results for:
<%=h @q -%>
<% end -%>
<% end -%>