Dan
Dan

Reputation: 1161

ERB is not getting some values of a local hash

I am using Sinatra and rendering views with ERB. I have the following action

get '/user/:id' do
  u = @users.retrieve( params[:id] )
  u[:mykey] = [1,2,3]
  erb( :user, :locals => { :user => u } )
end

and the view looks like this

<body>
    <h1><%= user["name"] %></h1>

    <pre><%= user["mykey"].to_json %></pre>

and where I expect to get the [1,2,3] array, I get a big fat null. Primitive values such as the name, are passed without a problem.

Upvotes: 0

Views: 1309

Answers (1)

JohnColvin
JohnColvin

Reputation: 2509

Is this of class Hash or HashWithIndifferentAccess?

You are setting user[:mykey] and retrieving user["mykey"]. Use symbol or string, don't mix them unless you are using HashWithIndifferentAccess.

Upvotes: 3

Related Questions