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

B

Ben Simms

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, :eek:rder => '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
 
P

Paul Mucur

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, :eek:rder => 'date DESC',
:conditions => ['title=?', params[:titleinput]])

respond_to do |format|
format.html
format.xml { render :xml => @events }
end
end
 
J

James Britt

Ben said:
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

Iñaki Baz Castillo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top