17

say I have a text field like the following in a view called 'search':

 <%= text_field_tag(:lookup) %>

how do I submit this ':lookup' value and pass it into the controller called 'search' and assign it to a variable?

It's a basic problem, but being a noob, it's difficult ;)

1 Answer 1

25

That will be accessible in the controller as

params[:lookup]

Your controller could look something like this:

class SearchesController < ActionController::Base

  def search
    lookup = params[:lookup]
    @models = Model.find_by_lookup(lookup)
  end
end

And your view should look like this:

<%= form_tag searches_path do %>
  <label for="lookup">Lookup</label>
  <%= text_field_tag :lookup %>
 <%= submit_tag "Submit" %>
<% end %>
Sign up to request clarification or add additional context in comments.

4 Comments

I forgot to mention in the question, but how do I submit the form?
arrg I must be stupid today. Am I supposed to create a controller file called searches_controller.rb?
Yes. If you're just getting started using scaffolding as an example might help. fairleads.blogspot.com/2007/12/…
Don't forget the equals sign before form_tag so that the form actually shows up! <%= form_tag searches_path do %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.