Forum: Ruby on Rails Proble wuth form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jose vicente R. (Guest)
on 2009-03-13 13:21
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:   <b>Permalink:</b>
8:   <%=h @encuesta.permalink %>
9: </p>
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?
Sumanth K. (Guest)
on 2009-03-13 13:31
(Received via mailing list)
Hey,

  Please refer to correct usage of form_for:
API:
http://api.rubyonrails.org/classes/ActionView/Help...
with examples:
http://apidock.com/rails/ActionView/Helpers/FormHe...

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:

http://sumanthtechsavvy.blogspot.com/
         http://www.twincling.org/node/227

http://yourway2health.blogspot.com/
           http://www.osef.twincling.org
Jose vicente R. (Guest)
on 2009-03-13 19:41
Sumanth Krishna wrote:
> Hey,
>
>   Please refer to correct usage of form_for:
> API:
> http://api.rubyonrails.org/classes/ActionView/Help...
> with examples:
> http://apidock.com/rails/ActionView/Helpers/FormHe...
>
> 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
Jose vicente R. (Guest)
on 2009-03-13 22:22
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

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 %>

  <p>
    <%= f.label :texto %><br />
    <%= f.text_field :texto %>
  </p>
   ...
 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:   <p>
7:     <%= f.label :texto %><br />
8:     <%= f.text_field :texto %>
9:   </p>
10:   <p>
11:     <%= f.label :encuesta_id %><br />



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??
Jose vicente R. (Guest)
on 2009-03-13 22:44
 I tried with:

 form_for [@encuesta, @pregunta] do |f|
  <%= f.error_messages %>

   <p>
     <%= f.label :texto %><br />
     <%= f.text_field :texto %>
   </p>
    ...
  end

 The result is the same. I thinking that perhaps i can0t do "restful"
forms :(
Jose vicente R. (Guest)
on 2009-03-13 23:03
Jose vicente Ribera pellicer wrote:
>  I tried with:
>
>  form_for [@encuesta, @pregunta] do |f|
>   <%= f.error_messages %>
>
>    <p>
>      <%= f.label :texto %><br />
>      <%= f.text_field :texto %>
>    </p>
>     ...
>   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!!
This topic is locked and can not be replied to.