POST params recognised but NULs entered in table?

Can someone please help. I do HTTP POST with 3 data items the log
below shows they
are recognised but the save into the table is enterng NULs. Indeed a
query on the db then shows a bunch of NULs. Why isn't the correct
paramer data not being entered? Thanks very much if you can help.

Processing ClientsController#create (for 192.168.2.101 at 2008-01-24
21:34:46) [POST]
  Session ID: 62a7094f0366f46f4a6c6702a08763ac
  Parameters: {"waypoints"=>"petem", "action"=>"create",
"landtitle"=>"blah", "controller"=>"clients", "owner"=>"petem"}
  ^[[4;36;1mClient Columns (0.001285)^[[0m ^[[0;1mSHOW FIELDS FROM
clients^[[0m
  ^[[4;35;1mSQL (0.000165)^[[0m ^[[0mBEGIN^[[0m
  ^[[4;36;1mSQL (0.000411)^[[0m ^[[0;1mINSERT INTO clients
(`waypoints`, `landtitle`, `owner`) VALUES(NULL, NULL, NULL)^[[0m
  ^[[4;35;1mSQL (0.007088)^[[0m ^[[0mCOMMIT^[[0m
Redirected to http://localhost:3000/clients/73
Completed in 0.01897 (52 reqs/sec) | DB: 0.00895 (47%) | 302 Found
[http://localhost/clients]

Please post your controller method

Can someone please help. I do HTTP POST with 3 data items the log
below shows they
are recognised but the save into the table is enterng NULs. Indeed a
query on the db then shows a bunch of NULs. Why isn't the correct
paramer data not being entered? Thanks very much if you can help.

Well where it's going wrong is either the controller or the model, but
you haven't shown us either of those :slight_smile:

Fred

Well where it's going wrong is either the controller or the model, but
you haven't shown us either of those :slight_smile:

Sorry, it's standard code from controller as per ...

# POST /clients
  # POST /clients.xml
  def create
    @client = Client.new(params[:client])

    respond_to do |format|
      if @client.save
        flash[:notice] = 'Client was successfully created.'
        format.html { redirect_to client_url(@client) }
        format.xml { head :created, :location =>
client_url(@client) }
      else
        format.html { render :action => "new" }
        format.xml { render :xml => @client.errors.to_xml }
      end
    end
  end

okay please post the new action and your form template

that is to say The error is obvious but those file will help point it
out

Well where it's going wrong is either the controller or the model,
but
you haven't shown us either of those :slight_smile:

Sorry, it's standard code from controller as per ...

# POST /clients
# POST /clients.xml
def create
   @client = Client.new(params[:client])

Your form's not right (or at least not in sync with your controller):
your controller is expecting stuff in params[:client] but from your
log the parameters for client were right at the top level of params.

Fred

"those file"?

I'm having the same problems:

Processing LabelsController#create (for 127.0.0.1 at 2008-01-24
15:53:01) [POST]
  Session ID: b34c741748c8da668ea66c5c2c9d639d
  Parameters: {"name"=>"system1", "memory"=>"2048", "serial"=>"12345",
"date"=>"2008-01-24", "action"=>"create", "controller"=>"labels",
"processor"=>"intel"}
  ←[4;35;1mLabel Columns (0.000000)←[0m ←[0mSHOW FIELDS FROM
`labels`←[0m
  ←[4;36;1mSQL (0.000000)←[0m ←[0;1mBEGIN←[0m
  ←[4;35;1mLabel Create (0.000000)←[0m ←[0mMysql::Error: #23000Column
'date' cannot be null: INSERT INTO `labels` (`name`, `memory`, `serial`,
`date`, `processor`) VALUES('', '', '', NULL, '')←[0m
  ←[4;36;1mSQL (0.000000)←[0m ←[0;1mROLLBACK←[0m

I am using the standard scaffolded POST method in the controller, and
the model is blank, as per scaffolding. Creating new objects works from
the web interface, but these values are being submitted from an external
ruby script:

require 'net/http'

Net::HTTP.start('localhost', 3000) do |query|
  print query.post("/labels",
"name=#{ENV['computername']}&serial=1234&date=2008-01-24&processor=intel&memory=2048").body
end

The only change I have made from the scaffolding is to turn off the
authentication token by putting

    protect_from_forgery :except => [:create]

in the controller.

Any ideas? Thanks.

Oh ... just a noob at this, I perhaps have the wrong idea. I have no
form as such since the idea is that this be on the receivng end of a
HTTP request (REST etc). The "new" is in the Client's ActiveRecord
subclass. Is totally overboard? BTW if I do access the /clients page
via a browser, I'm able to manage the table quite well as expected,
but my aim for for unattended HTTP requests to be serviced. Ta for
now.

Well Ian, seems we're on our own for now.

Look, I'm just a noobie and I've been coming back to this problem
several times over the last weeks and I'm out of ideas. Looks like an
internal RoR bug to me but no one else seems bothered. If you make any
progess could you please share it. Thanks.

It's not a rails bug, it's just a mismatch between the data your controller is expecting and the data you're giving it
the url Ian is hitting is name

That does it for me Fred. Thanks very, very much.

Thanks for pointing out the error of my ways Fred. Just as a follow-up
to anyone down the line who has this problem, I'll explain how I fixed
it for my app. First I spent some time building a new constructor for my
model that accepted a flat array as an initializer. Then I said, no,
this really belongs in the controller. But after I thought about making
a new model object in the controller and jamming things into it, I said,
that's kind of ugly too. Really, what if I want to pass the controller
two model objects at the same time down the line? The way the Rails team
intended it is right, the submitter just needs to indicate what model
object it is sending parameters for. So I extended the Hash class like
so:

class Hash
  def to_controller_post(controller)
    str = ""
    self.each { |key,value| str << "#{controller}[#{key}]=#{value}&" }
    str.chop #Remove trailing '&'
  end
end

Now my submit code looks like this:

def getAuditData
  {
    "name" => ENV['computername'],
    "username" => ENV['username'],
    "serial" => getSerialNumberBIOS,
    "processor" => getProcessorName,
    "memory" => getSystemMemory,
    "image" => getImageVersion
  }
end

Net::HTTP.start('localhost', 3000) do |query|
  query.post("/labels", audit_data.to_controller_post('label')).body
end

It produces URLs formed like this:

http://localhost:3000/labels?label[name]=labelsystem1&label[username]=bob.smith&label[serial]=12345&label[processor]=Intel&label[memory]=2048&label[image]=30

This works fine. If you are implementing this in your own code, do
remember that you will need to turn authentication token off for the
create action by putting

protect_from_forgery :except => [:create]

in your controller definition.

As a final note, I would be using the debugger, but I can't get the gem
installed under win32 because it requires Visual c++ 6.0 to compile,
which is not a free download. A little while back I wrestled with trying
to get it to compile under Visual C++ 9.0 Express, which is free, but
never got it working. Anyone happen to have a plug-and-play debugger
solution for win32?

Thanks again all.