Hi --
David Trasbo wrote:
Actually I think I'm close to a solution now. I made these two methods:
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")
That syntax will work but it would be much more idiomatic to do:
m << f.hidden_field(:name, :index ...)
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|
f.label :value, label, :index => nil, :for => id+"value"
f.text_field :value, :index => nil, :id => id+"value"
end
end
In my view I'm calling the second method (text_field_for_setting) like
this:
= text_field_for_setting(:site, :owner)
The method that is called here then calls my first method
(fields_for_setting) with a block. This actually works fine, but for
some reason that I can't figure out (I hope you can help me) It only the
LAST line of the block that is passed to fields_for_setting that shows
up.
In other words, even though I declare both a label and a text field,
only the text field shows up. How comes?
Because a block, like a method call, evaluates to the last expression
inside it. For example:
def get_string
str = yield
puts str
end
get_string do
"First string"
"Second string"
end
This will print "Second string". "First string" is just thrown away.
So maybe inside your block you want to concatenate the two strings.
David
···
On Thu, 16 Oct 2008, David Trasbo wrote:
--
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!