简体   繁体   English

如何调试不会更新或创建的 Rails3 model?

[英]How to debug a Rails3 model that won't update or create?

Help, I've broken my Rails3 user model!帮助,我破坏了我的 Rails3 用户模型!

I first noticed that I could not update users from the app itself.我首先注意到我无法从应用程序本身更新用户。

It appears that I also can't from the rails console.看来我也不能从 rails 控制台。

User.save returns false . User.save返回false

User.create produces a record with id=nil , which is not saved to the database. User.create生成一条id=nil的记录,该记录不会保存到数据库中。

I've spent two days trying to work out what the problem is.我花了两天时间试图找出问题所在。 I've taken out all my CanCan and Devise validations.我已经取出了我所有的 CanCan 和 Devise 验证。 I've been through the code line by line.我已经逐行浏览了代码。 I am stumped!我难住了!

Nothing in the logs is pointing me in an obvious direction.日志中的任何内容都没有为我指明一个明显的方向。

I commented everything out of User.rb, but no joy.我评论了 User.rb 中的所有内容,但没有任何乐趣。

I know this is a really vague question, that's almost impossible to answer from a far.我知道这是一个非常模糊的问题,几乎不可能从远处回答。

What I'm hoping is that someone can suggest logical steps for working through this and trying to figure out what's happening.我希望有人可以提出合理的步骤来解决这个问题并试图弄清楚发生了什么。

I'm still fairly new to Rails, and keen to learn how it all works.我对 Rails 还很陌生,并且热衷于了解它是如何工作的。

Thanks for your ideas!谢谢你的想法!

UPDATE:更新:

I've been digging into this for the past week.在过去的一周里,我一直在研究这个。 Eventually I started a new rails app from scratch and systematically moved the old app into the new app piece by piece to try and work out where the problem lies.最终,我从头开始了一个新的 Rails 应用程序,并系统地将旧应用程序逐个移入新应用程序,以尝试找出问题所在。

I've tracked it down to my custom Devise routes.我已经追踪到我的自定义 Devise 路线。

routes.rb路线.rb

devise_for :users,  :path_prefix => 'registration', :controllers => {:registrations => 'users/registrations'}

If i remove the:controllers line, then I can update the users name using the Devise user edit form.如果我删除:controllers 行,那么我可以使用 Devise 用户编辑表单更新用户名。

However this is not a workable solution.然而,这不是一个可行的解决方案。 The Devise edit form only provides name and email fields, but there are several other columns in my user model that the user needs access to. Devise 编辑表单仅提供名称和 email 字段,但我的用户 model 中还有其他几列用户需要访问。 Hence the custom routing.因此自定义路由。

So I've tracked the issue this far, but I'm not sure where to look yet.所以到目前为止我已经跟踪了这个问题,但我还不确定在哪里看。 I tried editing the registrations_controller to the default values, but no luck.我尝试将 registrations_controller 编辑为默认值,但没有运气。 (I even cut as pasted the code directly from the devise git repository). (我什至直接从 devise git 存储库中剪切粘贴代码)。

def edit
  super
end

def update
  super   
end

So I don't think the problem is the controller.所以我认为问题不在于 controller。

Which leaves the form itself.这留下了表格本身。 The form is defined as形式定义为

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :multipart => true }) do |f| %>

I can't see any issue with this.我看不出这有什么问题。 And I can't think where else to look to debug this.而且我想不出还有什么地方可以调试这个。

To recap, the edit form displays as expected, user can edit information, click update, is redirected as expected, no errors are displayed, but the edited info is not saved.回顾一下,编辑表单按预期显示,用户可以编辑信息,单击更新,按预期重定向,没有显示错误,但编辑的信息没有保存。 The tail shows no SQL save being called.尾部显示没有 SQL 保存被调用。

Very stumped, and I'd appreciate any wild ideas or suggestions!非常难过,如果有任何疯狂的想法或建议,我将不胜感激!

Many thanks非常感谢

Try this to debug in the console:试试这个在控制台中调试:

require 'pp'
u = User.new
u.save
pp u.errors

This should give you the errors that prevent the user from being saved to the DB.这应该会为您提供阻止用户保存到数据库的错误。

I've eventually tracked this down to a stupid oversight on my part.我最终将此归结为我的愚蠢疏忽。

In the controller I had在 controller 我有

resource.update_attributes!(params[:resource_name])

when what I needed was当我需要的是

resource.update_attributes!(params[resource_name])

An edit I must have made at some point.我必须在某个时候进行过编辑。

A self inflicted issue that took two weeks of head scratching to resolve!一个自找的问题,花了两个星期挠头才解决!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM