Forum: Ruby on Rails Dynamic form? Not really!

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.
Gerard (Guest)
on 2005-12-31 14:12
(Received via mailing list)
Hi guru's out there,

Im happilly coding my first rails app, and all goes well. I find myself
manually coding in a particular field all the time (company_id), that
relates
the contact to a company. The total coding of this form seems somewhat
much.
Could somebody tell me what Rails power I'm possibly missing and stuborn
as I
am trying to do myself?

I was wondering if the first part of the form can simply be put in the
loop
somehow and still be a dropdownlist. So if anybody wants to shoot,
here's the
form code:

<%= start_form_tag :action => 'update_contact', :id => @contact %>
<table border="0" cellpadding="3" cellpadding="0">
<tr>
  <th>Field</th><th>Contents</th>
  </tr>
### First part - My 'manual' coded stuff
<%  odd_or_even = 0 %>
  <tr class="ListLine<%= odd_or_even %>"><td>Company</td>
  <td>
  <select name="contact[company_id]">
  <% @companies.each do |company| %>
    <option value="<%= company.id %>">
      <%= company.name %>
    </option>
  <% end %>
  </select>
  </td></tr>
### The cool dynamic part that shows the rest
<%
  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" />
<%= end_form_tag %>

Thanx a lot guys!

Regards,

Gerard.
--
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!
Hiroshi T. (Guest)
on 2005-12-31 14:39
(Received via mailing list)
Hi, Gerard,


On Sat, 31 Dec 2005 13:12:45 +0100
Gerard <removed_email_address@domain.invalid> wrote:

   <select name="contact[company_id]">
>   <% @companies.each do |company| %>
>     <option value="<%= company.id %>">
>       <%= company.name %>
>     </option>
>   <% end %>
>   </select>

you can replace above with following:
( if @companies are extracted from Company class (companies table) )

<%= @companies = Company.find(:all)
    collection_select(:contact, :company_id, @companies,
                      :id, :name )
%>


--
Hiroshi T. <removed_email_address@domain.invalid>
Kevin O. (Guest)
on 2005-12-31 15:23
You can also use the 'cycle' helper to do the table striping.

Do something like this...

<tr class="ListLine<%= cycle("0","1") %>">
Gerard (Guest)
on 2005-12-31 15:24
(Received via mailing list)
Hiroshi,

Great thanx. That worked.

But my more pressing question: Is it necessary to have the dropdownlist
generated seperately from the full field generation loop?

Regards,

Gerard.

On Saturday 31 December 2005 13:39, Hiroshi T. tried to type
something
like:
> <%= @companies = Company.find(:all)
>     collection_select(:contact, :company_id, @companies,
>                       :id, :name )
> %>

--
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!
Gerard (Guest)
on 2006-01-01 14:28
(Received via mailing list)
Kevin,

I did not digest the helper chapter yet in the Agile webdev book, but I
asume
you mean create a shareable function called cycle, and remove the code
from
the view?

Grtz n Thanx

Gerard.

On Saturday 31 December 2005 14:23, Kevin O. tried to type
something
like:
> You can also use the 'cycle' helper to do the table striping.
>
> Do something like this...
>
> <tr class="ListLine<%= cycle("0","1") %>">

--
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!
Kevin O. (Guest)
on 2006-01-01 15:16
Gerard wrote:
> Kevin,
>
> I did not digest the helper chapter yet in the Agile webdev book, but I
> asume
> you mean create a shareable function called cycle, and remove the code
> from
> the view?
>
> Grtz n Thanx
>
> Gerard.

'cycle' is a predefined helper for doing table striping.

See
http://api.rubyonrails.com/classes/ActionView/Help...
Gerard (Guest)
on 2006-01-01 17:04
(Received via mailing list)
Kevin,

Thanx! That'll be a nice cleanup in my views. Will take a look at that
.. :-)

And best wishes to you to!

(And ofcourse to all coders the world over!)


Grtz Gerard

On Sunday 01 January 2006 14:16, Kevin O. tried to type something
like:
> >
> > Gerard.
>
> 'cycle' is a predefined helper for doing table striping.
>
> See
> http://api.rubyonrails.com/classes/ActionView/Help...
>30

--
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!
This topic is locked and can not be replied to.