[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

New to RoR. Using text input as a parameter in query.

Ben Simms

4/4/2008 6:18:00 PM

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-....

4 Answers

Paul Mucur

4/4/2008 8:30:00 PM

0


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.
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


James Britt

4/4/2008 8:34:00 PM

0

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

Iñaki Baz Castillo

4/4/2008 8:36:00 PM

0

El Viernes, 4 de Abril de 2008, Ben Simms escribi=C3=B3:
> 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.

Hi, since RoR is a framework based on Ruby is better you ask this kind of=20
questions in a RoR maillist. Note that RoR adds its own "magic" to Ruby ;)

=2D-=20
I=C3=B1aki Baz Castillo

Ben Simms

4/4/2008 10:12:00 PM

0

Thanks, that did it. Sorry for the post in the wrong area.
--
Posted via http://www.ruby-....