Getting nil when you want "" (a blank string)

This has got to be something obvious that I am missing…

I have a remote form that has one value that is passed back to the
server from a text field. The form has a set of parameters passed to the
:url key called search_params. When the field is empty and the form is
submitted, I get

:my_val => “”

as expected when I examine the log. All is well with the world.

I need call that same action from a link_to_remote, in this case forcing
my_val to be empty (""). To do this, I do

search_params.merge(:my_val => “”)

Examining the search_params hash after the merge, I see that my_val is
“” (not nil). When the params arrive at the server, the log shows
:my_val => nil. Due to some other code downstream, nil causes operations
to fail (it really needs to be “”, not nil).

What am I missing here?

TIA,
Keith

Hi!

Rails needs to encode params into urls…
I’ve done some work with the Net::HTTP library, where i needed to encode
params into an url. I used CGI.escape(my_params_string) to encode it
properly…
I guess encoding an empty string into a url is a mission…

To hack it in the meantime, use:

params[:my_val].to_s

nil.to_s

RUBY-DOC QUOTE:

Always returns the empty string.

nil.to_s #=> “”

Hope this helps in the meantime…
Ciao!

Gustav P.
[email protected]