Ruby on Rails not adding record to database

。_饼干妹妹 提交于 2021-02-11 14:32:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!