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

**URL:** <https://www.ruby-forum.com/t/howto-accept-input-from-a-form-to-a-variable-not-a-database-field/145313>\
**Category:** Rails\
**Created:** [August 7, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/howto-accept-input-from-a-form-to-a-variable-not-a-database-field/145313 "2008-08-07T21:14:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [August 7, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/howto-accept-input-from-a-form-to-a-variable-not-a-database-field/145313/1 "2008-08-07T21:14:19Z")

</div>

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.

Regards,

Paul Thompson

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [August 7, 2008, 9:24pm UTC](https://www.ruby-forum.com/t/howto-accept-input-from-a-form-to-a-variable-not-a-database-field/145313/2 "2008-08-07T21:24:18Z")

</div>

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  
text\_field\_tag.

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

On Aug 7, 3:13 pm, “Paul Jonathan T.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [August 7, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/howto-accept-input-from-a-form-to-a-variable-not-a-database-field/145313/3 "2008-08-07T21:54:09Z")

</div>

2008/8/8 Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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  
> text\_field\_tag.
> 
> 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 %\>

&nbsp;
\<%= button\_to 'Cancel', :action =\> 'index' %\>

And partial \_test.

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?

Thanks,

Paul Thompson.

| 
## Enter Data:
 | | \<%= text\_field\_tag, 'Field1' %\> |

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [August 7, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/howto-accept-input-from-a-form-to-a-variable-not-a-database-field/145313/4 "2008-08-07T22:16:23Z")

</div>

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 }

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

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

> **[Ruby on Rails Screencasts - RailsCasts](http://railscasts.com/)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

On Aug 7, 3:53 pm, “Paul Jonathan T.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [August 8, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/howto-accept-input-from-a-form-to-a-variable-not-a-database-field/145313/5 "2008-08-08T17:17:58Z")

</div>

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  
params[:hidden\_extra]

On Aug 7, 6:20 pm, “Paul Jonathan T.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [August 8, 2008, 12:21am UTC](https://www.ruby-forum.com/t/howto-accept-input-from-a-form-to-a-variable-not-a-database-field/145313/6 "2008-08-08T00:21:03Z")

</div>

2008/8/8 Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> def 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. [http://railscasts.com/](http://railscasts.com/)

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

Thanks,

Paul Thompson

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [August 13, 2008, 8:29am UTC](https://www.ruby-forum.com/t/howto-accept-input-from-a-form-to-a-variable-not-a-database-field/145313/7 "2008-08-13T08:29:40Z")

</div>

Hi Robert,

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

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.

Regards,

Paul Thompson.

2008/8/9 Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
