I am trying to rewrite my website in Rails 3 and Ruby 1.9.2.
Can some one help me convert my observe_field which is working under
Rails 2.3.5 to Rails 3. I would like to avoid using JQuery as over the
last year everything I have tried in JQuery has failed, including this
one. Perhaps due to my lack of knowledge. I would not like to install
the legacy upgrade.
What I have in my website is several pages(models) which use the same
search text field. Each controller has an action “search” which
renders a partial “_searchresults” on the page with the text field.
The code which works well in Rails 2.3.5 along with one typical
example search action from a controller and the “_searchresults” is
shown below:-
In the searches index.heml.erb:-
***************************************…
Just type the word or part of the word and wait two seconds
<% if params[:area] != nil%>
<% form_tag({:action => “foo”},{:id => “search”}) do %>
<%= label_tag(:query, “Search for:”) %>
<%= text_field_tag(“query”, params[:query], :autocomplete =>
“off” , :onKeyPress=>“return disableEnterKey(event)”) %>
<% end %>
<%= observe_field 'query', :frequency => 2,
:update => "search_results",
:url => {:controller => params[:area], :action =>
“search”,:area => params[:area]},
:method=>:get,
:with => “query” %>
(Search results will be listed here)
<%end%>In one of the controller:-
******************************…
def search
session[:query] = params[:query].strip if params[:query]
if session[:query] and request.xhr?
@plants_sn = Plant.find(:all, :conditions => [“scientificname
LIKE ?”, “%#{session[:query]}%”], :order => “commonname ASC”)
@plants_cm =Plant.find(:all, :conditions => [“commonname LIKE ?”,
“%#{session[:query]}%”], :order => “commonname ASC”)
@plants_desc =Plant.find(:all, :conditions => [“description
LIKE ?”, “%#{session[:query]}%”], :order => “commonname ASC”)
@plants = (@plants_cm + @plants_sn + @plants_desc).uniq
render :partial => "shared/searchresults", :layout =>
false, :locals => {:searchresults => @plants}
end
end
In Views/Shared/_searchresults:-
**********************…
<% if searchresults.length == 0 %>
Sorry! No articles found...
<% elsif params[:query] == “” %>
<p>You must enter a search term.</p>
<% else %>
<% if params[:area] == "plants" %>
<table>
<tr>**************.............elseif,elsfif etc,etc.
At the moment I am trying with a submit_tag, but I am getting a
missing template error. So I must solve this before I can continue
with the observe field.:-
<% end %>
Just type the word or part of the word and wait two seconds
<% if params[:area] != nil%>
<% form_tag url_for({:controller => params[:area], :action
=> :search, :area => params[:area] }), :method => “get”, :remote =>
true do %>
<%= label_tag(:query, “Search for:”) %>
<%= text_field_tag(“query”, params[:query], :autocomplete =>
“off” , :onKeyPress=>“return disableEnterKey(event)”) %>
<%= submit_tag(“Search”, :name => nil) %>
<% end %>