# Why do we use form\_for @story do |f| when there is no loop?

**URL:** <https://www.ruby-forum.com/t/why-do-we-use-form-for-story-do-f-when-there-is-no-loop/168689>\
**Category:** Rails\
**Created:** [May 24, 2009, 11:32am UTC](https://www.ruby-forum.com/t/why-do-we-use-form-for-story-do-f-when-there-is-no-loop/168689 "2009-05-24T11:32:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [May 24, 2009, 11:32am UTC](https://www.ruby-forum.com/t/why-do-we-use-form-for-story-do-f-when-there-is-no-loop/168689/1 "2009-05-24T11:32:23Z")

</div>

we use

(1…10).each do |i|  
p i  
end

so that a value is “yield” to i in a block…

but what about

\<% form\_for @story do |f| %\>  
\<%= f.text\_field %\>  
\<% end %\>

there is no loop at all… why do we need to make it look like a loop?  
Can’t we do it without making it look like a loop? (write in another  
way)?

Also, must be use a Story instance here? Can’t we just use :story and  
achieve the same result? The @story instance is just newly created and  
has no data at all – does it actually help creating the form? Can’t  
:story suffice already? thanks.

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [May 24, 2009, 11:38am UTC](https://www.ruby-forum.com/t/why-do-we-use-form-for-story-do-f-when-there-is-no-loop/168689/2 "2009-05-24T11:38:52Z")

</div>

On Sun, May 24, 2009 at 11:32 AM, SpringFlowers AutumnMoon  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has no data at all – does it actually help creating the form? Can’t  
> :story suffice already? thanks.

Both these examples of code use blocks, the first just happens to be a  
loop, the second only looks like a loop if your first experience of  
blocks is in loops.

I think it’s important that you forget about the loop and focus on  
blocks, once you’ve understood blocks, it will no longer look like a  
loop and you’ll understand that you really don’t want to write it a  
different way (even though it’s possible)

Yes, you can use @story or :story and form\_for will figure it out for  
you.  
Mostly it depends on where you’re using the form, new or edit etc.

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)

[http://MyMvelope.com](http://MyMvelope.com) - The SIMPLE way to manage your savings

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [May 24, 2009, 2:58pm UTC](https://www.ruby-forum.com/t/why-do-we-use-form-for-story-do-f-when-there-is-no-loop/168689/3 "2009-05-24T14:58:38Z")

</div>

Andrew T. wrote:

> On Sun, May 24, 2009 at 11:32 AM, SpringFlowers AutumnMoon  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 
> 
> I think it’s important that you forget about the loop and focus on  
> blocks, once you’ve understood blocks, it will no longer look like a  
> loop and you’ll understand that you really don’t want to write it a  
> different way (even though it’s possible)

can i use something like: (just pseudo code)

with (@story) do |f|  
f.begin\_form  
f.textfield :name  
f.end\_form  
end

so i think the block method will save the begin\_form and end\_form  
because it automatically add the begin and end before calling the block.  
is that the main benefit?

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [May 24, 2009, 3:08pm UTC](https://www.ruby-forum.com/t/why-do-we-use-form-for-story-do-f-when-there-is-no-loop/168689/4 "2009-05-24T15:08:12Z")

</div>

Marnen Laibow-Koser wrote:

> > so i think the block method will save the begin\_form and end\_form  
> > because it automatically add the begin and end before calling the block.  
> > is that the main benefit?
> 
> Oh, now I see what you mean about the syntax. Yes, that’s sort of it.  
> Read more about blocks in Ruby.

yes, what i meant was something like

```
with_model_give_form (@story) do |f|
  f.begin_form
  f.text_field :name
  f.end_form
end

```

except the begin\_form and end\_form can be called within  
“with\_model\_give\_form”, so it can become

```
with_model_give_form (@story) do |f|
  f.text_field :name
end

```

so it is now the same thing as form\_for

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [May 24, 2009, 3:05pm UTC](https://www.ruby-forum.com/t/why-do-we-use-form-for-story-do-f-when-there-is-no-loop/168689/5 "2009-05-24T15:05:56Z")

</div>

SpringFlowers AutumnMoon wrote:  
[…]

> can i use something like: (just pseudo code)
> 
> with (@story) do |f|  
> f.begin\_form  
> f.textfield :name  
> f.end\_form  
> end

You already are. Just replace “with” with “form\_for” and you’ll see  
that the syntax is essentially the same.

> so i think the block method will save the begin\_form and end\_form  
> because it automatically add the begin and end before calling the block.  
> is that the main benefit?

Oh, now I see what you mean about the syntax. Yes, that’s sort of it.  
Read more about blocks in Ruby.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
