Showing a Form

Thanks to Erol for giving me input on how to move stuff to a namespace.
meant some work, so next time I will spend more time thinking before
blasting off.

I’ve stored a Form in my DB which has Elements. Below is my
All it does is show.

class FormsController < ApplicationController
def show
@form = Form.find(params[:id])
@elements = Element.find(:all, :conditions => { :form_id => @form })

respond_to do |format|
  format.html # show.html.erb



I get stuck creating the view. It should go through all of the Form’s
elements and display a label with the Element’s name, and a text field
the Element’s value.

Right now I have this:

<% form_for @form, :url => { :action => ‘create’ } do |f| %>

<%= %> <% form_for(@form) do |f| %> <% for element in @elements %> <%= f.label_tag 'value', 'name' %> <%= f.text_field_tag 'value', element.value %> <% end %> <%= f.submit 'Submit' %> <% end %> <% end %>

I get this error:

undefined method `label_tag’ for

Whenever you’re using form_for(anObject), you want to use FormHelpers:

Whenever you’re using a standalone form that’s not tied to a model’s
attributes (IE, one in which you do not pass the FormConstructor into
the block as ‘f’, on which you’d call f.text_field ) you want to use

You’re using form_for with an object, which means you should use the
‘tagless’ versions of the form constructors, but instead you’re using
f.text_field_tag. The correct syntax is:

f.text_field :attribute_name, {value => element.value}

However, since you’re linking up associations in your form you’ll want
to use the fields_for helper, to bring the constructor into scope
while still referring to the proper element in the form. Read about it

Now, since you can set up Forms to ‘has_many’ Elements, and Elements
‘belong_to’ a Form, you can make your query like this in your

@form = Form.find(params[:id], {:include => :elements})

And then have the following code in your controller (Or something like
it, play with it a bit, I’ve never done anything exactly like this
before) :

<%= form_for @form, :action => create do |f| %>
<% @form.elements.each do |element| %>
<% fields_for element do |e| %>
<%= e.label ‘value’ %>
<%= e.text_field :value %>
<% end %>
<% end %>
<%= submit_tag %>
<% end %>

Play with the params output of that, and adjust your create action

If that’s not pretty enough for you, use HAML:

= form_for @form, :action => create do |f|

  • @form.elemenst.each do |element|
    • fields_for element do |e|
      = e.label ‘value’
      = e.text_field :value
      = submit_tag

On Sep 22, 5:23 pm, “Commander J.” [email protected]