Hi All,
I am still trying to get the hang of passing information to controller.
I want to extend the rails getting started tutorial for searching.
(Peak Obsession).
The index page would be the main search form passing the parameters
to a view
controller and displayed with a view.rhtml page.
I have started with something from a blog that looked promising.
http://blog.teksol.info/articles/2005/10/31/building-the-sql-where-
clause-dynamically-in-rails
Here is what I have started with but not sure how to get the values into
the conditions correctly.
Thanks for any suggestions
Processing FriendsController#view (for 127.0.0.1 at 2005-11-30
00:14:11) [POST]
Parameters: {“commit”=>“Search”, “action”=>“view”,
“controller”=>“friends”, “person”=>{“city”=>“”, “name”=>“Dave”,
“state”=>“”}}
friends_controller.rb
def view
#initialize the conditions array: [‘1=1’]. This is to prevent a
problem later on when Rails
#evaluates the conditions option to find .condition. (from blog)
conditions = [‘1=1’]
puts @params[:person][‘name’] if @params[:person][‘name’] #
works fine get name
conditions << ‘name = :name’ if @params[:person][‘name’] # what
is needed here?
conditions << ‘city = :city’ if @params[:person][‘city’]
conditions << ‘state = :state’ if @params[:person][‘state’]
@person = Person.find(:first, :conditions => [conditions.join(’
AND '), params])
puts conditions
end
index.rhtml
<%= start_form_tag :action => ‘view’ %>
Name
<%= text_field 'person', 'name' %>
City
<%= text_field 'person', 'city' %>
State
<%= text_field 'person', 'state' %>
<%= submit_tag “Search” %>
<%= end_form_tag %>