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!
Hi, Gerard, On Sat, 31 Dec 2005 13:12:45 +0100 Gerard <mailing@gp-net.nl> 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 Takagi <gollum@hi-net.zaq.ne.jp>
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 Takagi 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!
You can also use the ''cycle'' helper to do the table striping. Do something like this... <tr class="ListLine<%= cycle("0","1") %>"> -- Posted via http://www.ruby-forum.com/.
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@hi-net.zaq.ne.jp> On Sat, 31 Dec 2005 15:44:17 +0100 Gerard <mailing@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@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. > > --
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 Olbrich 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!
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/Helpers/TextHelper.html#M000430 -- Posted via http://www.ruby-forum.com/.
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 Olbrich tried to type something like:> 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/Helpers/TextHelper.html#M0004 >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!