Howto accept input from a form to a variable (not a database field)

How do I use a form to capture data for a variable that is no a
database field? In the case, I would like to search my database based
on the data that the user inputs via a form. Any assistance will be
greatly appreciated.


Paul Thompson

First use form_tag rather than form_for (which is designed for model
based forms). The instead of something like f.text_field you would use

Then just pull the parameter out of params by name
params[:name_of_field] and put that into your variable.

Hi Robert,

thanks for the guidance, I am pretty green when it comes to rails so
one more question if you don't mind? How do I kick this off? I have
this in my views folder.

<% form_tag :action => 'update', :id => @supplier do %>
  <%= render :partial => 'test' %>
  <%= submit_tag 'Save' %>
<% end %>
<div class="separator">&nbsp;</div>
<%= button_to 'Cancel', :action => 'index' %>

And partial _test.

<div class="form1">
<table width="30%" boader="8" cellspacing="2" cellpadding="2">
  <td align="left"><h2><label for="enter_data" >Enter Data: </label></h2></td>
  <td align="left"><td><%= text_field_tag, 'Field1' %></td>


Obviously "<% form_tag :action => 'update', :id => @supplier do %>"
needs to be changed as there is now no supplier database. Can you
please set me straight?


Paul Thompson.

Ok. To expand on that a bit say you wanted to add a search field to
find suppliers.

1. Add a collection action to your suppliers resource:

map.resources :suppliers, :collection => { :search => :get }

2. Add the action to your suppliers_controller.rb:

def search
  @suppliers = Supplier.find_by_name(params[:search])

  respond_to .... # the standard respond_to stuff here

3. In your view use your search action route and a text_field_tag.

<% form_tag(search_suppliers_path) do %>
  <%= text_field_tag :search
  <%= submit_tag 'Search' %>
<% end %>

This is untested code off the top of my head, but check the Rails API
docs for form_tag and test_field_tag for more details on usage. Also
this is only one of many solutions. It all depends on your needs, but
this is the simplest example I can think of.

Also if you don't subscribe to the Railscasts podcast. Check it out.
It has very good information to help get you started.

Hi Robert,

I would really, really love to be able to benefit from Railscasts
podcast. However I am totally deaf and so I am unable to use what must
be a marvellous resource.

I think that I have not explained properly what I want to do as the
answer that you have provided seem very complex for what is a small
thing. At the end of the day, all I want to do is accept a name from
an input field in a view. then store it in a session variable so that
I can use it to access various tables in a database when the user
decides what reports they want. Sorry if I have led you up the garden


Paul Thompson

Ahh. I see. The answer is still lurking in those previous responses.

You can still use form_field_tag inside of a form_for block.

So if you had something like:

<% form_for @suppliers do |form| %>
  <%= form.text_field :name %>

  <%= text_field_tag :extra %> #using a text_field_tag here because
it's not tied to a model
  <%= hidden_field_tag :hidden_extra, "Some hidden value" %> #or even
use a hidden_field_tag

<% end %>

Then in the controller access :extra by params[:extra] or

Hi Robert,

I just want to say thank you, very very much. Your last piece of
advise did the trick perfectly :slight_smile:

Sorry that it has taken me so long to get back to you but I was side
tracked by other issues. I am just one guy looking after IT for
company of fifty employees and I do everything. So sometimes I have to
put one thing aside and concentrate on another.

Thank you once again for the trouble that you have taken. It appreciated.


Paul Thompson.