问题
It's been a while since i dipped into Ruby on Rails and i'm having a hard time getting data into my model,
here's what the server is saying when i submit my form:
Processing ScoresController#index (for 127.0.0.1 at 2010-03-26 15:31:44) [POST]
Parameters: {"commit"=>"Add", "authenticity_token"=>"326dd05ffa596bfa12ec3ebb6f48933dbad8dc0c", "score"=>{"name"=>"third score", "score"=>"4"}}
Score Load (0.5ms) SELECT * FROM "scores" ORDER BY score DESC
Rendering scores/index
Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/scores]
but the record is not added to the database, i know this is really simple stuff but im stumped as to why it's not working!
you can see the source code on my github here: http://github.com/AlexEdwardFish/Scores/tree/master/app/
回答1:
It may not be passing validations, and your controller doesn't check for if save fails.
Also, you posted a log for index, but not for create... but post is wrong for index, ah ha! It's not running the create method. Probably a routing issue. Ah yes, you have your map.resources after the default routing statements. These two lines should be the last lines in the file, if used at all:
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
Why don't you use a resource generator to write a default scaffold for you? It's much easier to learn from an example that already works. :)
回答2:
Looks like you're submitting the form to index instead of create. Try rearranging your routes.rb file. Priority is based on creation, so the order there should be map.resources, map.root, then the map.connect lines.
map.resources :scores
map.root :controller => "scores"
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
来源:https://stackoverflow.com/questions/2525139/ruby-on-rails-not-adding-record-to-database