Gene Kahn
2006-Aug-05 05:36 UTC
[Rails] How to code partial search value in a find command
Hello,
In a list view, I''m trying to list a subset of a table''s
records based
on a partial search value. So if the user enters " Smith " then the
view
should list all records with " Smith " as the value of the relevant
field. I have this partial listing of the Find view:
Enter name to find
<%= start_form_tag :action => ''find'' %>
<p>
<%= text_field_tag :name, params[:name] %>
</p>
<%= link_to "Find It", :class => "submit" %>
<%= end_form_tag %>
Then, in the controller, I have this action:
def find
...
user = User.find_by_name(:all, :conditions => "name like
''%#{params[:name]}%''")
...
end
The result: no records found, despite some records satisfying the query.
Where did I go wrong?
Thanks for the help,
gk
--
Posted via http://www.ruby-forum.com/.
Michael Modica
2006-Aug-05 06:12 UTC
[Rails] Re: How to code partial search value in a find command
A little different, but here is an example where I build the search
based on many criteria (i''m pretty sure I found this somewhere on the
internet):
Controller:
@whereClause = ""
@params[:lead].each { |key, value |
if value.length > 0
@whereClause = @whereClause.length == 0 ? "#{key} LIKE
''%#{value}%''" :
@whereClause << " and #{key} LIKE
''%#{value}%''"
end
}
My view resembles yours, except I use:
<%= text_field ''lead'', ''first_name''
%>
I think your problem is with the following line:
''%#{params[:name]}%
should be (based on what I think):
''%#{params[:model_name][:name]}%
Hope this helps...
Michael
--
Posted via http://www.ruby-forum.com/.
Gene Kahn
2006-Aug-05 13:29 UTC
[Rails] Re: How to code partial search value in a find command
Michael Modica wrote:> A little different, but here is an example where I build the search > based on many criteria (i''m pretty sure I found this somewhere on the > internet): > > Controller: > > @whereClause = "" > @params[:lead].each { |key, value | > if value.length > 0 > @whereClause = @whereClause.length == 0 ? "#{key} LIKE ''%#{value}%''" : > @whereClause << " and #{key} LIKE ''%#{value}%''" > end > } > > My view resembles yours, except I use: > > <%= text_field ''lead'', ''first_name'' %> > > I think your problem is with the following line: > > ''%#{params[:name]}% > > should be (based on what I think): > > ''%#{params[:model_name][:name]}% > > Hope this helps... > > MichaelHi, Thanks for the suggestion. I tried a number of permutations, but it didn''t work for me either. gk -- Posted via http://www.ruby-forum.com/.