# Understanding Form\_for

**URL:** <https://www.ruby-forum.com/t/understanding-form-for/189207>\
**Category:** Rails\
**Created:** [May 26, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/understanding-form-for/189207 "2010-05-26T20:09:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [May 26, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/understanding-form-for/189207/1 "2010-05-26T20:09:33Z")

</div>

OK, so I’m an absolute noob at Ruby on Rails. I’ve read a lot of  
information I can find about form\_for and most of them use form\_for to  
create new items that get saved into the database and can be viewed  
later to edit, destroy, etc. In this case, the database will fill up  
with whatever the user wrote and I just want my program to have no  
information stored.

I’ve been trying to take a user’s input from the text\_field or  
file\_field and once they hit the “submit” button, they get redirected  
to another page that, for instance, says “You wrote: [insert user  
input here]” I’ve been having no luck whatsoever doing this except to  
use HTML form, but I was hoping I can do this in Ruby on Rails.

Thanks for any help,  
anon\_comp

---

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [May 26, 2010, 8:43pm UTC](https://www.ruby-forum.com/t/understanding-form-for/189207/2 "2010-05-26T20:43:30Z")

</div>

On 26 May 2010 19:06, anon\_comp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> input here]" I’ve been having no luck whatsoever doing this except to  
> use HTML form, but I was hoping I can do this in Ruby on Rails.

If the form is not associated with a model then form\_tag may be more  
appropriate.

Colin

---

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [May 26, 2010, 9:06pm UTC](https://www.ruby-forum.com/t/understanding-form-for/189207/3 "2010-05-26T21:06:20Z")

</div>

On May 26, 2:41 pm, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If the form is not associated with a model then form\_tag may be more  
> appropriate.

Going by your suggestion, Colin, my project looks like this

trying\_controller.rb:  
class TryingController \< ApplicationController  
def index  
end

def to  
@name = params[:title]  
end  
end

index.html.erb:  
\<% form\_tag :action =\> :to %\>  
Name:  
\<%= text\_field\_tag :name, params[:name] %\>  
\<%= submit\_tag “OK” %\>

to.html.erb:  
Name: \<%= @name %\>

The submit button doesn’t work…

---

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [May 27, 2010, 4:18pm UTC](https://www.ruby-forum.com/t/understanding-form-for/189207/4 "2010-05-27T16:18:59Z")

</div>

On 26 May 2010 22:01, anon\_comp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On May 26, 4:30 pm, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > In what way does it not work?
> 
> When I hit the “OK” button nothing happens, it stays on the same page.
> 
> > Did you check the html to see if it is what you expect?
> 
> I ran the server and looked at the page if that’s what you mean, yes.

I meant that you should check that the html for the form is correct  
html for a form with a submit button. I think you may find that there  
is a where you do not expect it, (separate from the one you  
have manually added).

Looking at your code you have got the use of form\_tag wrong.  
Unfortunately you have snipped your original code so it is difficult  
to insert the corrections. Have another look at the docs for  
form\_tag, you will see there should be a ‘do’ there and there is no  
need to add the yourself.

> > I think it should be :action = ‘to’ rather than :to
> 
> It didn’t affect anything, I beleive they mean the same thing.

They don’t mean the same thing, though sometimes they are  
interchangeable. I am not sure in this case, again check the html to  
see if it is correct.

Colin

---

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [May 27, 2010, 4:31pm UTC](https://www.ruby-forum.com/t/understanding-form-for/189207/5 "2010-05-27T16:31:17Z")

</div>

On 26 May 2010 22:15, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I meant that you should check that the html for the form is correct  
> html for a form with a submit button. I think you may find that there  
> is a where you do not expect it, (separate from the one you  
> have manually added).

Use View, Page Source, or similar, in your browser to view the html.

Colin

---

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [May 27, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/understanding-form-for/189207/6 "2010-05-27T16:40:39Z")

</div>

Problem solved.

Thank you very much Colin for the excellent help 🙂

I just had to change the def to in my controller

class TryingController \< ApplicationController  
def index  
end

def to  
@name = params[:name]  
end  
end

---

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [May 26, 2010, 10:31pm UTC](https://www.ruby-forum.com/t/understanding-form-for/189207/7 "2010-05-26T22:31:48Z")

</div>

On 26 May 2010 20:04, anon\_comp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> \<%= submit\_tag “OK” %\>
> 
> to.html.erb:  
> Name: \<%= @name %\>
> 
> The submit button doesn’t work…

In what way does it not work?

Did you check the html to see if it is what you expect?

I think it should be :action = ‘to’ rather than :to

Also when things are not working as you expect the log file  
(log/development.log) can often be useful in analysing the problem.

Colin
