From: "David A. Black" Date: 2008-10-17T19:25:52+09:00 Subject: Re: A Ruby block question Hi -- On Fri, 17 Oct 2008, David Trasbo wrote: > David Trasbo wrote: > >>> So maybe inside your block you want to concatenate the two strings. >> >> Okay, I'm using #concat instead. It works just fine, thanks! > > I must have overseen something. Today it doesn't work very well. I do > get my label and text field but I don't get the surrounding

tag and > the hidden fields. My methods looks like this: > > def fields_for_setting(namespace, name) > id = "settings_#{namespace}_#{name}_" > m = Builder::XmlMarkup.new :indent => 2 > fields_for "settings[]", setting = > Setting.find_or_initialize_by_namespace_and_name(namespace, name) do |f| > m.p do > unless setting.new_record? > m << (f.hidden_field :id, :index => nil, :id => id+"id") > > end > m << (f.hidden_field :namespace, :index => nil, :id => > id+"namespace") > m << (f.hidden_field :name, :index => nil, :id => id+"name") > m << yield(f) > end > end > end > > def text_field_for_setting(namespace, name, label=nil) > namespace = namespace.to_s > name = name.to_s > label ||= "#{namespace.capitalize} #{name}" > id = "settings_#{namespace}_#{name}_" > fields_for_setting(namespace, name) do |f| > concat f.label :value, label, :index => nil, :for => id+"value" > concat f.text_field :value, :index => nil, :id => id+"value" > end > end > > So, I'm concatenating the label and text field inside the > fields_for_setting block. But the fields_for_setting block doesn't have > any effect at all! I think it's because fields_for_setting is only > _returning_ the

and hidden fields, but how is this solved? Try this: fields_for_setting(namespace, name) do |f| f.label :value, label, :index => nil, :for => id+"value" + f.text_field :value, :index => nil, :id => id+"value" end i.e., returning a string from the block. David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL Advancing with Rails January 19-22 Fort Lauderdale, FL * * Co-taught with Patrick Ewing! See http://www.rubypal.com for details and updates!