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

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html

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
FormTagHelpers:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html

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
here:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M001741

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
Controller:

@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
accordingly.

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 Johnson" <[EMAIL PROTECTED]>
wrote:
> Hi,
> Thanks to Erol for giving me input on how to move stuff to a namespace. It
> 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 FormsController.
> 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
>     end
>   end
>
> end
>
> 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 with
> the Element's value.
>
> Right now I have this:
>
> <% form_for @form, :url => { :action => 'create' } do |f| %>
>   <fieldset>
>     <legend><%= @form.name %></legend>
>     <% 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 %>
>   </fieldset>
> <% end %>
>
> I get this error:
>
> undefined method `label_tag' for #<ActionView::Helpers::FormBuilder:0xbcd7ee>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to