I'm new to RoR and I am trying to figure out how to use user input from
a text field as a parameter in a query. I know this is probably simple
but I have been messing around with it for a while and can't figuire it
out.
The controller has this method:
def search
@events = Event.find(:all, :order => 'date DESC',
:conditions => ['title=?',titleinput])
respond_to do |format|
format.html
format.xml { render :xml => @events }
end
end
and the view:
<form action='/events/search' method='post'>
<p>
<b>Title</b></br>
<input id='event_title' name='titleinput' size='30' type='text'
value=''/>
<input type='submit' value='Search'/>
</p>
</form>
I know this is probably completely wrong but I couldn't figure out how
to use the variable from the form in the query in the controller.
Thanks for any help.
Ben
···
--
Posted via http://www.ruby-forum.com/.
In Rails, variables in forms are stored in the params hash so, in your case, to get the value of titleinput you would do params[:titleinput]. This would change your controller's search method to the following:
def search
@events = Event.find(:all, :order => 'date DESC',
:conditions => ['title=?', params[:titleinput]])
respond_to do |format|
format.html
format.xml { render :xml => @events }
end
end
···
On 4 Apr 2008, at 19:17, Ben Simms wrote:
I know this is probably completely wrong but I couldn't figure out how
to use the variable from the form in the query in the controller.
Ben Simms wrote:
I'm new to RoR and I am trying to figure out how to use user input from
a text field as a parameter in a query.
In general, Rails questions are better asked on the Rails mailing list.
···
--
James Britt
"In Ruby, no one cares who your parents were, all they care
about is if you know what you are talking about."
- Logan Capaldo
Hi, since RoR is a framework based on Ruby is better you ask this kind of
questions in a RoR maillist. Note that RoR adds its own "magic" to Ruby 
···
El Viernes, 4 de Abril de 2008, Ben Simms escribió:
I'm new to RoR and I am trying to figure out how to use user input from
a text field as a parameter in a query.
--
Iñaki Baz Castillo
Thanks, that did it. Sorry for the post in the wrong area.
···
--
Posted via http://www.ruby-forum.com/.