A rant about parameters

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

Sometimes, I get frustrated too (I miss intellisense :).

Usually, a method is a related closely to another. As in the case of
country_select, I see that there’s a related method,
“country_options_for_select”, which mentions the array of countries. You
have to do a little lookup, which I really hate. I would rather have the
docs be repetitive as long as each parameter is explained well.

“John W.” [email protected] wrote
in message news:[email protected]

I’m honestly seeking your advice here…how do you keep the various
different ways methods, paritcularly ActionView helpers, can be called
straight?

It seems pretty rare to me that this sort of ambiguity happens,
clearly it does but I hadn’t run into it personally since I started.
So my answer is that I don’t remember the intricacies. I don’t know
how one would push this up the stack to DHH himself but it seems like
a good thing to change asap.

Can anyone tell me how value here could ever have a value?

InstanceTag is much more complex than the api would have you believe.
There is a function called value in the InstanceTag class which fills
this role. For reference, InstanceTag is around 150 lines of code. The
source on the api is hidden for a reason I think. :slight_smile:

Don’t give up, Rails is extremely new. Each release changes subtle
things like this and makes it much better in my opinion. The
difference between 0.13, 1.0.0, and 1.1.12 has been like night and day
but the change seemed to happen in no time at all.

Ta,
Chuck V.

On 7/8/06, John W. [email protected] wrote:

|can’t convert nil into String|

docs or method signature, what kind of variables the method takes? This
module ActionView::Helpers

http://lists.rubyonrails.org/mailman/listinfo/rails

I feel your pain, and I think there room for a lot of improvement in
the docs for cases like this. I suggest you write a patch with added
documentation, and submit it on the dev site:
http://dev.rubyonrails.org/ - also include the specific problem you
had due to the lack of docs, to explain why the patch is needed.
Documentation patches are pretty easy to get applied if they make
sense - send a msg to the rails-core list to spur it on.

This is open source, after all =).

On Jul 8, 2006, at 4:38 PM, John W. wrote:

Can anyone tell me how value here could ever have a value?

Thanks for listening. I feel better somehow, but still not clean.

Do you believe that value is a local variable, rather than a method call
on self?


– Tom M.

Tom M. wrote:

Do you believe that value is a local variable, rather than a method call
on self?

Yes, I did. Looking at the code, I can’t for the life of me find a
“value” method. Am I looking in the wrong place?

I appreciate the discussion. However, this thread illustrates what I
feel is a real weakness in Rails. In a language as dynamic as Ruby, you
really have to have documentation with each method specifying the
contract of that method…what the method expects, and what the method
returns. Without this, claims of productivity may be somewhat
premature…if you have to research code for methods to determine what
sort of values they expect and can handle, your producivity will go down
quickly…trust me.

I understand lack of documentation (sometimes) if you’re writing a
framework for internal consumption, but if you’re writing it for public
consumption the game changes. It seems perhaps the Rails team should
develop a set of loose coding standards, where no code will be excepted
unless a proper method contract is included for each one.

Just my two cents…I think it would help tremendously. PHP and Java
have already conquered this problem…surely Ruby/Rails can?

Thanks,
John

I made the same mistake of assuming that value was a local variable,
while it is indeed a method. It’s defined in:

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_view/helpers/form_helper.rb

def value
unless object.nil?
object.send(@method_name)
end
end

On Jul 10, 2006, at 8:01 AM, John W. wrote:

Tom M. wrote:

Do you believe that value is a local variable, rather than a
method call
on self?

Yes, I did. Looking at the code, I can’t for the life of me find a
“value” method. Am I looking in the wrong place?

snip…

Just my two cents…I think it would help tremendously. PHP and Java
have already conquered this problem…surely Ruby/Rails can?

Since Rails is a community based open source project, feel free to
contribute where you think it needs improvement.


– Tom M.

John W. wrote:

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?

I think ‘value’ is a method call, not a variable.

A.

Tom M. wrote:

On Jul 10, 2006, at 8:01 AM, John W. wrote:
Since Rails is a community based open source project, feel free to
contribute where you think it needs improvement.

Tom,

While I agree with you that it is indeed an open project and that, if
one sees issues, one should take up cause and contribute, I think
enforcing proper documentation of methods (expected parameters and
returns) is something that has to be done an enforced by the project
leads. I cannot personally dictate that all contributed code or
implemented code have this…but DHH and the commit gatekeepers can.

Understand, I’m not trying to criticize…more, I’m trying to suggest
what would help developers like me and yourself leverage the framework
in a more efficient way. I am a cheerleader for Rails and want to see it
succeed.

I think just saying it’s open source, go contribute is not really
addressing the problem.

Thanks,
John

Mike G. wrote:

I made the same mistake of assuming that value was a local variable,
while it is indeed a method. It’s defined in:

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_view/helpers/form_helper.rb

Aha…thanks Mike. I made the wrong assumption that the definition of
InstanceTag in form_options_helper.rb was the only definition…need to
remember to grep “class ClassName” from all possible source files.

John