> --
> You received this message because you are subscribed to the Google Groups
> "sinatrarb" group.
> To post to this group, send email to sina...@googlegroups.com.
> To unsubscribe from this group, send email to
> sinatrarb+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sinatrarb?hl=en.
>
Sinatra's params object is a Hash, not the HashWithIndifferentAccess
you may be used to from Rails. However, there seems to be something
clever going on behind the scenes, since both params[:foo] and
params['foo'] return the same value. Try running this:
require 'rubygems'
require 'sinatra'
get "/" do
"#{params.class}: #{params.inspect}<br>" +
"params[:foo] = #{params[:foo]}<br>" +
"params['foo'] = #{params['foo']}<br>"
end
open http://localhost:4567/?foo=bar
and you'll see that even though it's a Hash, you get both :foo and
'foo' returning "bar". So what you're describing may be an actual bug.
Can you reproduce it?
---
Alex Chaffee - al...@cohuman.com - http://alexch.github.com
Stalk me: http://friendfeed.com/alexch | http://twitter.com/alexch |
http://alexch.tumblr.com
We use Hash's default value proc to allow symbol or string access to
params. You can see the code that makes this work here:
http://github.com/sinatra/sinatra/blob/30c2ef8/lib/sinatra/base.rb#L533-543
The param keys are stored as strings and there's a slight performance
hit when using symbols.
Thanks,
Ryan
> ---
> Alex Chaffee - al...@cohuman.com - http://alexch.github.com
> Stalk me: http://friendfeed.com/alexch | http://twitter.com/alexch |
> http://alexch.tumblr.com
>
Okay, it gets messy because we are trying to stuff the hash into Mongodb
using the Ruby driver (I'll copy Kyle Banker in on this)
we create an initial hash var using strings
doc = {"name" => nil, "email" => nil, "active" => true}
then it gets updated with colons (strings were crashing)
doc[:username] => "joe"
doc[:email] => "j...@foo.com"
then we push this to a save method which does:
doc = {}
doc["username"] = params[:username]
doc["email"] = params[:email]
doc["active"] = params[:active]
......
coll.save(doc) # mongo update
=> active does not get updated
I tried to use all strings (no colons) but that gave errors - thus the
confusing mix of colons and strings - something in the sinatra code
seems to want colons
see if this makes any sense and if anyone sees an easy workaround (or
simply a best practice) - if not, I'll try to break out the lib code and
put in the test file to post.
So I was right that there was some cleverness, plus that is hands down
the best link URL i've ever seen.
1. How does it "crash" when you use strings? String as keys should work fine. You're going to have to provide more info on this. A pastie with a test case would be best.
2. If 'active' isn't being saved, you need to show the exact hash being sent to #save. Again, a test case is best here.