[Rails] Dynamic form? Not really!

Hiroshi Takagi gollum at hi-net.zaq.ne.jp
Sun Jan 1 02:13:34 GMT 2006


Gerard,

Now I've got what you mean by 'the loop', I guess :-)

>   for column in Contact.content_columns
>   odd_or_even = 1 - odd_or_even
> %>
>   <tr class="ListLine<%= odd_or_even %>">
>   <td><%= column.human_name %>:</td> 
>   <td><%= text_field 'contact', column.name  %></td>
>   </tr>
> <% end %>

the loop 
-----------
for column in SomeModel.content_columns
  column.human_name
  text_field 'somemodel', column.name
end
----------
was generated by scaffold, right?
scaffolded forms are not compatible with associated/related fileds,
cause Rails CAN NOT realize whether they are associated or not.
theres no general method in Rails, I'm afraid.

In fact I always throw away scaffolded forms except for some simple
master table maintenance. I always programmed by hand forms like you
have, or if I found that I would do too much REAPEAT ( thats against DRY
primary directive ), I programmed some generators for special purpose.

Copy scaffold templates to your ~/.rails/generators and hack and modify
templates as you like.

Regards,


Hiroshi Takagi <gollum at hi-net.zaq.ne.jp>


On Sat, 31 Dec 2005 15:44:17 +0100
Gerard <mailing at gp-net.nl> wrote:

> Hiroshi,
> 
> Thanx for you patience! What I mean with the loop is that this piece of code:
> 
> <%
>   odd_or_even = 0
>   for column in Contact.content_columns
>   odd_or_even = 1 - odd_or_even
> %>
>   <tr class="ListLine<%= odd_or_even %>">
>   <td><%= column.human_name %>:</td> 
>   <td><%= text_field 'contact', column.name  %></td>
>   </tr>
> <% end %>
> </table>
> <input type="submit" value="Save" />
> 
> shows all the fields from my Contact model. The 'for' code makes it a loop. 
> But when the company_id field is in there it shows a "#<Company:0x408ba4d0>" 
> in a text field. Since it is a related field, it must be a dropdown list. 
> What bugs me is that I have to put in a seperate pice of code for relational 
> fields. In the example, the one that you helped clean up:
> 
> <%  odd_or_even = 0 %>
>   <tr class="ListLine<%= odd_or_even %>"><td>Company</td>
>   <td><select name="contact[company_id]">
>     <%= @companies = Company.find(:all)
>         collection_select(:contact, :company_id, @companies, :id, :name )
>     %>
>   </select></td>
>   </tr>
> 
> I was wondering if Rails could figure this out for me. So that I only need the 
> loop in the top pice of code and still maintain the relation between records.
> 
> Regards,
> 
> Gerard.
> 
> 
> On Saturday 31 December 2005 15:28, Hiroshi Takagi tried to type something 
> like:
> > Gerald,
> >
> > On Sat, 31 Dec 2005 14:24:11 +0100
> >
> > Gerard <mailing at gp-net.nl> wrote:
> > > But my more pressing question: Is it necessary to have the dropdownlist
> > > generated seperately from the full field generation loop?
> >
> > Sorry, I can't understand whats you mean by 'the full filed generation
> > loop'. my English is damn poor thing :-(
> >
> > do you want this dropdownlist generated automatically?
> > if so, try to make some generator for your views.
> 
> -- 






More information about the Rails mailing list