From: Stefano Crocco Date: 2008-10-16T20:47:25+09:00 Subject: Re: A Ruby block question Alle Thursday 16 October 2008, David Trasbo ha scritto: > Stefano Crocco wrote: > >> In other words I need to put user-defined input in between some > >> pre-defined content. How can I approach that? > > > > If I understand correctly what you want to do, you don't need to do > > anything > > special: > > > > def my_method > > puts "predefined text 1" > > puts yield > > puts "predefined text 2" > > end > > My method looks like this: > > def fields_for_setting(namespace, name, &block) > namespace = namespace.to_s > name = name.to_s > 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") > #puts yield f??? > end > end > end > > The problem is, I'm using Builder::XmlMarkup and the fields_for from > Rails. I want to put some hidden fields AND the text fields defined by > the block into a

tag. That means I need to be able to access the f > object provided by fields_for and use it in this method's block. > > But how? I don't know Rails and Builder::XmlMarkup, so I can be completely wrong. From a quick look at Builder::XmlMarkup api, I'd say that, if the block returns the string you want to insert in the p element, you only have to insert it in m like you did for the texts: m << yield f Stefano