A Ruby block question

David A. Black wrote:

... 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.

syntax error, unexpected tSYMBEG, expecting kEND

But I grabbed your concept, this actually works:

def fields_for_setting(namespace, name)
  output = ""
  id = "settings_#{namespace}_#{name}_"
  m = Builder::XmlMarkup.new(:indent => 2, :target => output)
  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
  puts output
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|
    o = f.label(:value, label, :index => nil, :for => id+"value")
    o << f.text_field(:value, :index => nil, :id => id+"value")
  end
end

But, come on! There must be a better solution. This doesn't seem very
idiomatic to me. Nobody got a better idea?

···

--
Posted via http://www.ruby-forum.com/\.

I skipped through most of this thread, so this might be off target,
but have you looked into using Rails' "capture" helper to get a block
of Haml into a string?
http://api.rubyonrails.com/classes/ActionView/Helpers/CaptureHelper.html#M001750

···

On Fri, Oct 17, 2008 at 7:32 PM, David Trasbo <davidtrasbo@gmail.com> wrote:

But, come on! There must be a better solution. This doesn't seem very
idiomatic to me. Nobody got a better idea?

Henrik --- wrote:

But, come on! There must be a better solution. This doesn't seem very
idiomatic to me. Nobody got a better idea?

I skipped through most of this thread, so this might be off target,
but have you looked into using Rails' "capture" helper to get a block
of Haml into a string?
http://api.rubyonrails.com/classes/ActionView/Helpers/CaptureHelper.html#M001750

No, actually I haven't. Thank you very much for your suggestion. I'm
using that now. (:

···

--
Posted via http://www.ruby-forum.com/\.