# Proble wuth form

**URL:** <https://www.ruby-forum.com/t/proble-wuth-form/162753>\
**Category:** Rails\
**Created:** [March 13, 2009, 12:21pm UTC](https://www.ruby-forum.com/t/proble-wuth-form/162753 "2009-03-13T12:21:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chimorito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@chimorito](https://www.ruby-forum.com/u/chimorito)\
**Post date:** [March 13, 2009, 12:21pm UTC](https://www.ruby-forum.com/t/proble-wuth-form/162753/1 "2009-03-13T12:21:11Z")

</div>

Hi, forst at all sorry if my english is not the best. I have a problem  
with a form. I use REST routes, encuestas has\_many preguntas and  
preguntas belongs\_to encuesta and has\_many votados.  
I want to show in a form all the respuestas in one encuesta. I tried  
with this

\<%form\_for [@encuesta, @preguntas] do |f|%\>  
hola  
\<%- end -%\>

But it doesnÂ´t work. This is the error:

RuntimeError in Encuestas#show

Showing encuestas/show.html.erb where line #10 raised:

Called id for nil, which would mistakenly be 4 – if you really wanted  
the id of nil, use object\_id

Extracted source (around line #10):

7: **Permalink:**  
8: \<%=h @encuesta.permalink %\>  
9:

  
10: \<%form\_for [@encuesta, @preguntas] do |f|%\>  
11:  
12: hola  
13:
* * *

If i try with something like this:  
\<%for pregunta in @encuesta.preguntas %\>  
hello  
\<%end%\>  
I can see hello so many times like preguntas are in encuesta. But i need  
a form\_for.

Anybody can help me please?

---

<div class="post-metadata">

**Author:** ![chimorito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@chimorito](https://www.ruby-forum.com/u/chimorito)\
**Post date:** [March 13, 2009, 12:31pm UTC](https://www.ruby-forum.com/t/proble-wuth-form/162753/2 "2009-03-13T12:31:24Z")

</div>

Hey,

Please refer to correct usage of form\_for:  
API:

> **[ActionView::Helpers::FormHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html)**
>
> Form helpers are designed to make working with resources much easier compared to using vanilla HTML.

with examples:  
[http://apidock.com/rails/ActionView/Helpers/FormHelper/form\_for](http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for)

On Fri, Mar 13, 2009 at 1:21 PM, Jose vicente Ribera pellicer \<  
removed\_email\_address@domain.invalid\> wrote:

> \<%- end -%\>  
> Extracted source (around line #10):
> 
> >

## –

Thanks & Regards,  
Sumanth Krishna. A  
+358 40 3276564

Blogs:  
TwinclingCommunity:

> **[TechSavvy](http://sumanthtechsavvy.blogspot.com/)**
>
> Technical Blog

[http://www.twincling.org/node/227](http://www.twincling.org/node/227)

[http://yourway2health.blogspot.com/](http://yourway2health.blogspot.com/)  
[http://www.osef.twincling.org](http://www.osef.twincling.org)

---

<div class="post-metadata">

**Author:** ![chimorito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@chimorito](https://www.ruby-forum.com/u/chimorito)\
**Post date:** [March 13, 2009, 6:41pm UTC](https://www.ruby-forum.com/t/proble-wuth-form/162753/3 "2009-03-13T18:41:30Z")

</div>

Sumanth Krishna wrote:

> Hey,
> 
> Please refer to correct usage of form\_for:  
> API:  
> [ActionView::Helpers::FormHelper](http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html)  
> with examples:  
> [form\_for (ActionView::Helpers::FormHelper) - APIdock](http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for)
> 
> On Fri, Mar 13, 2009 at 1:21 PM, Jose vicente Ribera pellicer \<  
> removed\_email\_address@domain.invalid\> wrote:
> 
> > \<%- end -%\>  
> > Extracted source (around line #10):
> > 
> > >
> 
> –

Partially solved.

\<%- end -%\> was write, but i didÅ„ copy it.

Thanks a lot

---

<div class="post-metadata">

**Author:** ![chimorito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@chimorito](https://www.ruby-forum.com/u/chimorito)\
**Post date:** [March 13, 2009, 9:44pm UTC](https://www.ruby-forum.com/t/proble-wuth-form/162753/4 "2009-03-13T21:44:35Z")

</div>

I tried with:

form\_for [@encuesta, @pregunta] do |f|  
\<%= f.error\_messages %\>

\<%= f.label :texto %\>  
 \<%= f.text\_field :texto %\>

 ... end

The result is the same. I thinking that perhaps i can0t do “restful”  
forms ☹

---

<div class="post-metadata">

**Author:** ![chimorito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@chimorito](https://www.ruby-forum.com/u/chimorito)\
**Post date:** [March 13, 2009, 9:22pm UTC](https://www.ruby-forum.com/t/proble-wuth-form/162753/5 "2009-03-13T21:22:50Z")

</div>

Umm i have a problem.  
my routes.rb :

map.resources :encuestas do |encuesta|  
encuesta.resources :preguntas do |pregunta|  
pregunta.resources :solucions  
end  
end

I can see perfecly the index of preguntas  
Now I want to create a new pregunta.  
My url-\>[http://localhost:3000/encuestas/1/preguntas/new](http://localhost:3000/encuestas/1/preguntas/new)

In the preguntas’s controller:

def index  
@encuesta = Encuesta.find(params[:encuesta\_id])  
@preguntas = Pregunta.find(:all)

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @preguntas }
end

```

In the view of new pregunta:

form\_for [@encuesta, @preguntas] do |f|  
\<%= f.error\_messages %\>

\<%= f.label :texto %\>  
 \<%= f.text\_field :texto %\>

 ... end

Encuesta has 2 fields, titulo and descripcion. Both are strings  
Pregunta has 3 fields, encusta\_id and orden are integer and text is  
string.

This is the error message:

NoMethodError in Preguntas#new

Showing app/views/preguntas/new.html.erb where line #8 raised:

undefined method `texto’ for #Encuesta:0x7ffe122c40a8

Extracted source (around line #8):

5:  
6:

7: \<%= f.label :texto %\>  
  
8: \<%= f.text\_field :texto %\>  
9:

  
10: 

11: \<%= f.label :encuesta\_id %\>

Encuesta doesn’t has texto, texto is a field of preguntas. This form is  
for preguntas not for encuesta. Why is supossing that texto is a field  
of encuesta??

---

<div class="post-metadata">

**Author:** ![chimorito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@chimorito](https://www.ruby-forum.com/u/chimorito)\
**Post date:** [March 13, 2009, 10:03pm UTC](https://www.ruby-forum.com/t/proble-wuth-form/162753/6 "2009-03-13T22:03:30Z")

</div>

Jose vicente Ribera pellicer wrote:

> I tried with:
> 
> form\_for [@encuesta, @pregunta] do |f|  
> \<%= f.error\_messages %\>
> 
> \<%= f.label :texto %\>  
> \<%= f.text\_field :texto %\>
> 
> ... end
> 
> The result is the same. I thinking that perhaps i can0t do “restful”  
> forms ☹

Solved!!

\<% form\_for(@pregunta, :url =\> encuesta\_preguntas\_path(@encuesta)) do  
|f| %\>

It rules!!
