Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Form Values into a controller in Rails

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 ;)

like image 716
Adam Lee Avatar asked Aug 20 '09 20:08

Adam Lee


1 Answers

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 %>
like image 81
erik Avatar answered Oct 22 '22 18:10

erik