Guys,
I’ve been chasing a problem with country_select for the past few hours
now. My intention was pretty simple…use the following line in a form:
Country <%= f.country_select :country_region, "United States" %>
However, for the life of me, I couldn’t get it to work. I kept getting
|can’t convert nil into String|
Turns out, after much heartache, that my problem was I wasn’t passing it
as an array. It should have been
<%= f.country_select :country_region, [“United States”] %>
Now, looking at the api docs, it says:
country_select(object, method, priority_countries = nil, options = {},
html_options = {})
Return select and option tags for the given object and method, using
country_options_for_select
http://api.rubyonrails.com/classes/ActionView/Helpers/FormOptionsHelper.html#M000406
to generate the list of option tags.
How was I to know it should have been an array? Had I not been so tired,
I would’ve probably infrerred it from the parameter’s pluralization.
Additionally, after figuring out the problem, I noted
country_options_for_selects doc does say priority_countries should be an
array. Still, I feel beaten. How do you know, from looking at the api
docs or method signature, what kind of variables the method takes? This
is one instance where I miss static typing…if this were Java code I
would’ve known in two seconds what the method expected.
I’m honestly seeking your advice here…how do you keep the various
different ways methods, paritcularly ActionView helpers, can be called
straight?
Compounding the problem (and confusing me) was this method further down
in the call stack:
module ActionView::Helpers
class InstanceTag
def to_country_select_tag(priority_countries, options,
html_options)
html_options = html_options.stringify_keys
add_default_name_and_id(html_options)
content_tag(“select”,
add_options(country_options_for_select(value, priority_countries),
options, value), html_options)
end
end
end
Can anyone tell me how value here could ever have a value?
Thanks for listening. I feel better somehow, but still not clean.
John