Request#params

Not sure what the intent is here in request.rb, module Raw, included in
class Context

31 def params
32 if method == :post
33 @post_params.instance_variable_get("@hash")
34 else
35 @get_params.instance_variable_get("@hash")
36 end
37 end

@post_params is itself a hash and an instance variable on Context, and
@post_params.instance_variable_get( “@hash” ) returns nil.

Anything wrong with doing this instead?

31 def params
32 return method == :post ? @post_params : @get_params
33 end

If that’s acceptable I’ll add it to the patches I’m preparing.

thx.

A second, related question –

OgAdminController#save attempts to access request data via the ‘request’
method call:

klass = name_to_class(request[‘object_class_name’].to_s)
obj.assign(request, :assign_relations => true, :force_boolean => true)

A couple problems

  • Major misnomer: request returns a @context object
  • @request does not have a [] method for accessing incoming request
    variables ( nor should it…)

I’ve worked around it for now by calling
@context.post_params[‘object_class_name’] … which led up to the
question in my prev. email, since @context.params would be nicer…

Whoa… I should proofread…

@request does not have a [] method” should read “@context does not
have…”

Hi,

OgAdminController#save attempts to access request data via the ‘request’
method call:

klass = name_to_class(request[‘object_class_name’].to_s)
obj.assign(request, :assign_relations => true, :force_boolean => true)

yes, I believe that was always like that, as a shortcut to
request.params.

A couple problems

  • Major misnomer: request returns a @context object

That I don’t know about.

  • @request does not have a [] method for accessing incoming request
    variables ( nor should it…)

Any reason why?

  • request.params[‘foo’]
  • request[‘foo’]

I use the second form all the time.

I’ve worked around it for now by calling
@context.post_params[‘object_class_name’] … which led up to the
question in my prev. email, since @context.params would be nicer…

I’m not sure I follow you here. The parameters come from the “Request”
(requested by browser) and I’m not sure if ‘context’ better describes
where the parameters come from. IMO they belong to the current
‘request cycle’ and not to a ‘generic context’. But mind you, that is
just generally, if Nitro returns a Context object when you call
request then that’s just kind of an alias…

31 def params
32 return method == :post ? @post_params : @get_params
33 end

I’m not sure about the contents of those, but IIRC the plan (quite a
while
back) was to do a little more:

def params
return method == :post ? @get_params.merge(@post_params) :
@get_params
end

(Where I assume the @get_params is an hash.)

But that might now be obsolete when we have both .get/post_params
available.

Carry on :stuck_out_tongue:

Jo

You’re right… but… still… I swear – ‘request[]’ failed in
nitro/part/admin/og/controller.rb in OgAdminController::save

I’ll try a very simple test case to verify – perhaps some code included
via raw/model/webfile.rb is overriding it somehow…

The accessors do work as advertised in this case:

#!/usr/bin/env ruby
require ‘nitro_and_og’
include Nitro

class Foo
def index
render_text "
request() returns #{request.nil? ? ‘nil’ : request.class.name }
Parameters
"
end
def paramtest(*args)
%{
params=#{request.params}

request[:foo] = #{request[:foo]}
}
end
end

app=Application.new
app.dispatcher.root = Foo
app.start

Resending

With a fresh darcs get ( 30 minutes ago ) and this test program:

#!/usr/bin/env ruby
require ‘ubygems’
require ‘sqlite3’
require ‘nitro_and_og’
include Nitro
require ‘nitro/part/admin’

Og model

class Book
attr_accessor :title, String
attr_accessor :author, String
end

Controller

class Foo
def index
render_text “Foo”
end
end

Og.create_schema = true
Og.use_uuid_primary_keys = true
Og.start( :name => “library”, :adapter => :sqlite, :evolve_schema =>
:full )

app=Application.new
app.dispatcher.root = Foo
app.start

Saving a ‘Book’ produces this exception:

ERROR: Error while handling OgAdminController#save()
ERROR: undefined method `[]’ for nil:NilClass

/Users/rmela/nitro2/repo.nitroproject.org/script/lib/…/…/raw/lib/raw/context/request.rb:304:in
`[]’

/Users/rmela/nitro2/repo.nitroproject.org/script/lib/…/…/nitro/lib/nitro/part/admin/og/controller.rb:93:in
`save’

Line 93 throws the exception:

92   def save
93     klass = name_to_class(request['object_class_name'].to_s)
94
95     if oid = request['oid']
96       obj = klass[oid.to_s]
97       obj.assign(request, :assign_relations => true,

:force_boolean => true)
98 else
99 obj = klass.new
100 obj.assign(request, :assign_relations => true)
101 end

I modified that slightly to produce some output:

92   def save
93     if request.nil?
94        puts "OGADMIN request returns 'nil'"
95     else
96        puts "OGADMIN request() returns a #{request.class.name}"
97        puts "OGADMIN request.params returns #{request.params ||

‘nil’}"
98 puts “OGADMIN request[‘oid’] returns #{request[‘oid’]}”
99 end

which produced this exception:

ERROR: undefined method []' for nil:NilClass /Users/rmela/nitro/repo.nitroproject.org/script/lib/../../raw/lib/raw/context/request.rb:304:in[]’
/Users/rmela/nitro/repo.nitroproject.org/script/lib/…/…/nitro/lib/nitro/part/admin/og/controller.rb:98:in
`save’

And this output:

OGADMIN request() returns a Raw::Context
OGADMIN request.params returns nil

@post_params/@get_params are Dictionary objects. And I needed a version
of
them as Hash.

Dictionary is not that compatible with Hash as someone would like.

-g.

  • request.params[‘foo’]
  • request[‘foo’]

I use the second form all the time.

I use request[“foo”] too…

btw you can also use:

request[:foo]

one more hidden 0.50.0 new feature?

-g.

It’s not working in OgAdminController.

Perhaps it’s some interaction with WebFile…?

Try OgAdminController with this model (written from memory – some
changes might be needed…)

In app.rb:

require ‘raw/model/webfile’

Model:

class Asset
attr_accessor :name, String
attr_accessor :file, WebFile
end