A Ruby block question

D

David Trasbo

David said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top